[thelist] Regexp help?

Simon Willison cs1spw at bath.ac.uk
Fri Sep 19 12:23:00 CDT 2003


Dougal Campbell wrote:
> I've been searching for a regexp that will let me do text replacements
> inside HTML text, but not within HTML tags themselves. In other words,
> if I had content like this:
> 
>   <a href="/meta/RSS.xml" title="My RSS feed">Our RSS 2.0 feed</a>
> 
> I want to be able to modify the text 'RSS' without breaking the link or
> the title attribute.

Aaaaar matey. That's one dirty dawg of a problem, but I vanquished it 
last night convertin' my blog to pirate. Here's the trick, ya scurvy 
land lubber:

function myFunction($bits) {
     $text = $bits[1];
     // Now run the replacements on $text
     // ...
     return '>'.$text.'<';
}

$text = preg_replace_callback('/>(.*?)</s', 'myFunction', $text);

That thar snippet be just the trick. Now down yer grog, fire up yer 
hornpipe and find yerself a comely wench. Ye international talk like a 
pirate is almost at an end!

Arrrrrr!

Simon
http://simon.incutio.com/



More information about the thelist mailing list