Prevent Widows with PHP and JavaScript

By  on  

One of the small touches you can add to your website is preventing "widows" in your H1-H6 tags.  For those who aren't aware, a widow (in terms of text and headings) means only one word of a title wraps to the next line -- a bit of an ugly sight if you ask me.  The way to prevent widows with just text is by adding a   between the last two words of the text instead of a regular space character.  Here are two snippets for preventing widows in your website:  one using JavaScript and another using PHP!

// With JavaScript
var text = text.replace(/\s(?=[^\s]*$)/g, ' ');
// With PHP
$text = preg_replace( '|([^\s])\s+([^\s]+)\s*$|', '$1 $2', $text);

As I mentioned originally, widows are not necessarily a bug, but a small visual quirk that just doesn't look great.  Keep these regex usages handy so you can prevent such a smudge!

Recent Features

  • By
    Vibration API

    Many of the new APIs provided to us by browser vendors are more targeted toward the mobile user than the desktop user.  One of those simple APIs the Vibration API.  The Vibration API allows developers to direct the device, using JavaScript, to vibrate in...

  • By
    I’m an Impostor

    This is the hardest thing I've ever had to write, much less admit to myself.  I've written resignation letters from jobs I've loved, I've ended relationships, I've failed at a host of tasks, and let myself down in my life.  All of those feelings were very...

Incredible Demos

  • By
    Digg-Style Dynamic Share Widget Using the Dojo Toolkit

    I've always seen Digg as a very progressive website. Digg uses experimental, ajaxified methods for comments and mission-critical functions. One nice touch Digg has added to their website is their hover share widget. Here's how to implement that functionality on your site...

  • By
    CSS Filters

    CSS filter support recently landed within WebKit nightlies. CSS filters provide a method for modifying the rendering of a basic DOM element, image, or video. CSS filters allow for blurring, warping, and modifying the color intensity of elements. Let's have...

Discussion

  1. Great idea to take care of all headings at once!

    Only concern I would have would be search engines. Are there repercussions to adding this markup? Would it confuse/deter proper search engine indexing?

    I’m sure Google, etc take javascript into account in some way, but I would do this via javascript instead of PHP to lessen the chances of hurting search rankings (if that’s important to you).

  2. I’ve never seen the ?= operator in regular expression. And I don’t find such in my regex cheat sheet. Can you please explain how this particular reg ex is working? Thanks.

  3. The ?= is a look-ahead operator. It allows you to specify an expression that matches what comes next. In the example above

    (?=[^\s]*$)

    the expression is stating that the character after the space must be zero-or-more non-whtie-space characters followed by the end of the string. In other words, it makes sure that it only replaces the last space in the heading with a non-breaking space.

  4. Guru

    PHP not cancer of the Web

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!