Gabriel
Gabriel

Reputation: 792

Php regex match a string between two html tags with the tags been unknown

Ok, so here's my issue:

I have a link, say: http://www.blablabla.com/watch?v=1lyu1KKwC74&feature=list_other&playnext=1&list=AL94UKMTqg-9CfMhPFKXPXcvJ_j65v7UuV

And the link is between two tags say like this:

<br>http://www.blablabla.com/watch?v=1lyu1KKwC74&feature=list_other&playnext=1&list=AL94UKMTqg-9CfMhPFKXPXcvJ_j65v7UuV<br></p>

Using this regex with preg_replace:

'@(^|[^\/]|[^>])('.addcslashes($link,'.?+').')([^\w\/]|[^<]$)@i'

As such:

preg_replace('@(^|[^\/]|[^>])('.addcslashes($link,'.?+').')([^\w\/]|[^<]$)@i', "***",$strText);

The resulted string is :

<br***p>

Which is wrong!!

It should have been

 <br>***<br></p>

How can I get the desired result? I have blasted my head out trying to solve this one out.

I would like to mention that str_replace replaces even the link within another valid link, so it's not a good method, I need an exact match between two boundaries, even if the boundary is text or another HTML tag.

Upvotes: 0

Views: 679

Answers (1)

cwied
cwied

Reputation: 96

Assuming you don't want to use a DOM parser for some reason, I believe doing what you intended is as simple as the following:

preg_replace('@(^|[^\/]|[^>])('.addcslashes($link,'.?+').')([^\w\/]|[^<]$)@i', "$1***$3",$strText);

This uses $1 and $3 to put back the delimiting text you matched in your regular expression.

As others have pointed out, using a DOM parser is more reliable.

Does this do what you want?

Upvotes: 1

Related Questions