Assign Anchor IDs Using PHP
Last week, I challenged my readers to create a PHP script that finds anchors in an HTML document and assigns an ID to the element IF the element doesn't have an ID already. Jeremy Parrish stepped up to the challenge.
The PHP
function anchor_fix($anchor)
{
// the match comes as an array
// the whole match (what we want) is the 0th element
if (! preg_match('/\sid="/i', $anchor[0])) {
return preg_replace('/name="([^"]*)"/i', 'id="$1" $0', $anchor[0]);
} else {
// already has an id!
return $anchor[0];
}
}
/* usage */
echo preg_replace_callback('/<a[^>]*>/i', 'anchor_fix', file_get_contents('page.html'));
The Result
<body>
<b>
<a
name="stuff">this is an anchor</a> some text... <a name="another">another one...</a>
</b>
<div><a id="thing" name="other">another thing</a>
</div>
</body>
... becomes ...
<body>
<b>
<a
id="stuff" name="stuff">this is an anchor</a> some text... <a id="another" name="another">another one...</a>
</b>
<div><a id="thing" name="other">another thing</a>
</div>
</body>
Great job Jeremy!
![Create a CSS Cube]()
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...
![Conquering Impostor Syndrome]()
Two years ago I documented my struggles with Imposter Syndrome and the response was immense. I received messages of support and commiseration from new web developers, veteran engineers, and even persons of all experience levels in other professions. I've even caught myself reading the post...
![Camera and Video Control with HTML5]()
Client-side APIs on mobile and desktop devices are quickly providing the same APIs. Of course our mobile devices got access to some of these APIs first, but those APIs are slowly making their way to the desktop. One of those APIs is the getUserMedia API...
![Facebook-Style Modal Box Using MooTools]()
In my oh-so-humble opinion, Facebook's Modal box is the best modal box around. It's lightweight, subtle, and very stylish. I've taken Facebook's imagery and CSS and combined it with MooTools' awesome functionality to duplicate the effect.
The Imagery
Facebook uses a funky sprite for their modal...