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!
![JavaScript Promise API]()
While synchronous code is easier to follow and debug, async is generally better for performance and flexibility. Why "hold up the show" when you can trigger numerous requests at once and then handle them when each is ready? Promises are becoming a big part of the JavaScript world...
![Creating Scrolling Parallax Effects with CSS]()
Introduction
For quite a long time now websites with the so called "parallax" effect have been really popular.
In case you have not heard of this effect, it basically includes different layers of images that are moving in different directions or with different speed. This leads to a...
![Create Twitter-Style Dropdowns Using MooTools]()
Twitter does some great stuff with JavaScript. What I really appreciate about what they do is that there aren't any epic JS functionalities -- they're all simple touches. One of those simple touches is the "Login" dropdown on their homepage. I've taken...
![Dynamically Create Charts Using MooTools MilkChart and Google Analytics]()
The prospect of creating graphics charts with JavaScript is exciting. It's also the perfect use of JavaScript -- creating non-essential features with unobtrusive scripting. I've created a mix of PHP (the Analytics class), HTML, and MooTools JavaScript that will connect to Google Analytics...