Jump to content

quick question about str_replace


the-botman

Recommended Posts

Hi ..

 

see i want to replace any url from the data and replace it with 1: this is the code i have using str_replace but i dont know what regex to use please help me

$changeurl = str_replace('<a href="http://what-regex to use here">', '1:', $changeurl);

so expl say it is  <a href="http//lovers.com"> it will display as 1:

 

Thanks in Advance

Botman

Link to comment
https://forums.phpfreaks.com/topic/183303-quick-question-about-str_replace/
Share on other sites

@rajivgonsalves (and CV as well)

While the odds are not high that tags that start with 'a' will not be anchor tags, I would recommend using a word boundery (and additionally, an href attribute - more on that in a sec) to help differentiate the anchor tag from other tags.. for example (using rajivgonsalves' pattern):

 

$content = <<<EOF
<acronym title="as soon as possible">ASAP</acronym><a href="[url=http://www.somesite.com]http://www.somesite.com[/url]">Some site link</a>
EOF;

echo preg_replace('#<a[^>]+>(.*?)</a>#', '$1', $content);

 

This would obviously yield incorrect results, as the pattern would in essence capture 'ASAP</acronym><a href="http://www.somesite.com">Some site link' and use that. Another issue that could arise is when using say an anchor tag as a css sprite image. So if there was an achor tag like <a class="cssSpriteSheet image01"></a> (in this case, a css rule would have an image url associated to the class cssSpriteSheet, and another css rule would isolate a section of that image as well as its positioning and width/height via image01), this would also be wiped out.

 

 

Making use of word bounderies in conjunction in a href attribute to ensure proper targeting:

 

 


$content = <<<EOF
<acronym title="as soon as possible">ASAP</acronym><a href="http://www.somesite.com">Some site link</a><a class="cssSpriteSheet image01"></a>
EOF;


echo preg_replace('#<a\b[^>]*href[^>]+>(.*?)</a>#', '$1', $content);

 

 

Obviously, there's many ways to skin a cat. This is one of them (I didn't bother using \K and lookahead assertions to eliminate the need to capture, just for simplicity's sake).

alternate pattern:

 

$content = preg_replace('~</?a[^>]*>~i', '', $content);

 

works 100% thanks alot for all your help

 

It would work perfectly assuming all your html tags that start with a are anchor tags that are used as links. If not, then that pattern will wipe out all tags that start with a (regardless to whether they are anchor tags (used as links or otherwise) or not. EDIT - If this is going to be an issue, see my previous post in the event you missed it.

@nrg:  yeah I thought of that.  Overall I opted to say fuck it who cares, as the 'exceptions' are pretty rare.  I suppose it's up to the OP to really determine that, based on the content he's working with.

 

I... I knew that.  ::)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.