the-botman Posted November 29, 2009 Share Posted November 29, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/183303-quick-question-about-str_replace/ Share on other sites More sharing options...
the-botman Posted November 29, 2009 Author Share Posted November 29, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/183303-quick-question-about-str_replace/#findComment-967552 Share on other sites More sharing options...
wildteen88 Posted November 29, 2009 Share Posted November 29, 2009 If you're going to be using regex then str_replace() is not the function you want, as it doesn't support regex. You'll need to use preg_replace instead. Quote Link to comment https://forums.phpfreaks.com/topic/183303-quick-question-about-str_replace/#findComment-967556 Share on other sites More sharing options...
rajivgonsalves Posted November 29, 2009 Share Posted November 29, 2009 its should be something like <?php $content = '<a href="cookie/7941-Its-tough-to-be-fascinating.">It\'s tough to be fascinating.</a>'; echo preg_replace('#<a[^>]+>(.*?)</a>#', '$1', $content); ?> Quote Link to comment https://forums.phpfreaks.com/topic/183303-quick-question-about-str_replace/#findComment-967567 Share on other sites More sharing options...
.josh Posted November 29, 2009 Share Posted November 29, 2009 alternate pattern: $content = preg_replace('~</?a[^>]*>~i', '', $content); Quote Link to comment https://forums.phpfreaks.com/topic/183303-quick-question-about-str_replace/#findComment-967577 Share on other sites More sharing options...
nrg_alpha Posted November 29, 2009 Share Posted November 29, 2009 @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). Quote Link to comment https://forums.phpfreaks.com/topic/183303-quick-question-about-str_replace/#findComment-967603 Share on other sites More sharing options...
the-botman Posted November 29, 2009 Author Share Posted November 29, 2009 alternate pattern: $content = preg_replace('~</?a[^>]*>~i', '', $content); works 100% thanks alot for all your help Quote Link to comment https://forums.phpfreaks.com/topic/183303-quick-question-about-str_replace/#findComment-967605 Share on other sites More sharing options...
nrg_alpha Posted November 29, 2009 Share Posted November 29, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/183303-quick-question-about-str_replace/#findComment-967609 Share on other sites More sharing options...
.josh Posted November 29, 2009 Share Posted November 29, 2009 @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. Quote Link to comment https://forums.phpfreaks.com/topic/183303-quick-question-about-str_replace/#findComment-967610 Share on other sites More sharing options...
nrg_alpha Posted November 29, 2009 Share Posted November 29, 2009 @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. Quote Link to comment https://forums.phpfreaks.com/topic/183303-quick-question-about-str_replace/#findComment-967611 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.