Prevent Widows with PHP and JavaScript
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!
It's no secret that Facebook has become a major traffic driver for all types of websites. Nowadays even large corporations steer consumers toward their Facebook pages instead of the corporate websites directly. And of course there are Facebook "Like" and "Recommend" widgets on every website. One...
Chances are that any Web designers using our Ghostlab browser testing app, which allows seamless testing across all devices simultaneously, will have worked with responsive design in some shape or form. And as today's websites and devices become ever more varied, a plethora of responsive images...
You've probably noticed that I shy away from writing really long articles. Here are a few reasons why:
Most site visitors are coming from Google and just want a straight to the point, bail-me-out ASAP answer to a question.
I've noticed that I have a hard time...
CSS cubes really showcase what CSS has become over the years, evolving from simple color and dimension directives to a language capable of creating deep, creative visuals. Add animation and you've got something really neat. Unfortunately each CSS cube tutorial I've read is a bit...
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).
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.
The
?=
is a look-ahead operator. It allows you to specify an expression that matches what comes next. In the example abovethe 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.
PHP not cancer of the Web