homepage Welcome to WebmasterWorld Guest from 54.198.224.121
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe and Support WebmasterWorld
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Regular Expression preg_replace
Ignore text within a href
humpo




msg:1290055
 2:26 pm on Dec 28, 2005 (gmt 0)

Trying to automatically insert links in text
cut down example..
<?PHP
$Text = "
blue!
<a href='/blue/black/'>black</a>
yellow";
$find="black";
$Text = @preg_replace("#\b($find)\b#is","<a href='someurl'>$1</a>",$Text,1);
?>
However, the code above will match text which is already in links.

Is there a way to replace text and have the expression ignore text in between hrefs - stripping html is not a solution unless you can put it back again, need to keep existing html intact.

cheers

 

coopster




msg:1290056
 10:59 pm on Jan 5, 2006 (gmt 0)

Here is something that may work, if the existing text is truly a full html text source (assumption based on there always being a beginning element and an ending element, if not, you would need to add a '>' to the beginning of the $string you are searching as well as an '>' to the end.
$pattern = "#>([^<]*)$find([^<]*<(?!/a>))#is"; 
$Text = preg_replace($pattern,">$1<a href='someurl'>$find</a>$2",$Text,1);

I think I have that right ;-)
The pattern says to find anything in the string that begins with a closing element tag '>' followed by zero or more of anything that is not an opening element tag '([^<]*)' -- and capture that part, followed by our search term, optionally followed by anything that is not an opening element tag followed by an opening element tag which is not the beginning of a closing <a> element '([^<]*<(?!/a>))' -- capturing this portion as well.

The captured parts get used in the replacement value as variables $1 and $2.

The biggest trick going on here is that we are using a negative lookahead assertion. You can read more about these fun tricks in the PHP PCRE Pattern Syntax [php.net] manual pages.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved