Jump to content

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

well what i need to do is remove from this code <a href="cookie/7941-Its-tough-to-be-fascinating.">It's tough to be fascinating.</a> and only print "It's tough to be fascinating." but the url will always be different.

@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.  ::)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.