[nycphp-talk] typical regex question
Fan, Wellington
wfan at VillageVoice.com
Tue Jul 20 17:52:47 EDT 2004
Maybe a combination of preg_split/preg_match would do it.
//// ALL UNTESTED CODE....
$pattern = "/title=\".+\"/U";
$text = 'Howdy! This is a long string with some <tag
title="howdy">pieces</tag> in it...';
// Unzip the big string into alternate pieces...
$texts_array = preg_split( $pattern, $text);
preg_match( $pattern, $text, $titles_array);
// Now $titles_array is an array of the pieces that match $pattern,
// and $texts_array is an array of everthing else.
// Do soemthing with the pieces of text...
function user_func(&$value, $key) {
$value = <DO SOMETHING>;
}
array_walk( $texts_array, 'user_func' ); // do something with the pieces...
// Zip it back up
for( $i = 0; $i < count($texts_array); $i++) {
$newString .= $texts_array[$i] . $titles_array[$i];
}
///////////////////////////////////////////////
You'd need to check (or force) that the $text doesn't begin with a match to
$pattern.
Essentially, this depends on _match and _split to be converses (?) of each
other -- the string comes apart like teeth in a zipper, then you process all
the teeth on one side, then zip it back up.
Hope that helps...
--
Wellington
> -----Original Message-----
....
> I'm having a little personal struggle with replacing selected words
> in a long article which is in HTML. I want to search for words and
> replace them with a link, but not if they are already inside a
> title="" attribute of a tag.
....
More information about the talk
mailing list