AV1611 Posted October 3, 2008 Share Posted October 3, 2008 $str='this is a string with some <a href="whatever.com">Some Link< / a > to a site'; $str=somephpfunction($str); echo $str; // this is a string with ***LINK DELETED*** to a site How would I do that? Quote Link to comment https://forums.phpfreaks.com/topic/126858-solved-eliminate-links/ Share on other sites More sharing options...
JasonLewis Posted October 3, 2008 Share Posted October 3, 2008 Well if you want to remove any links I suppose you could do it like so: preg_replace("#<a href=['\"]?.*?['\"]?>.*?</a>#i", "-LINK REMOVED-", $str); Quote Link to comment https://forums.phpfreaks.com/topic/126858-solved-eliminate-links/#findComment-656163 Share on other sites More sharing options...
AV1611 Posted October 3, 2008 Author Share Posted October 3, 2008 Thanks. I guess I asked the wrong question though... I don't really wanna eliminate the link. I either want the part between the <a>THIS</a> or maybe href="THIS" That's a much harder problem... the second one is bad cause it may have " ' or other characters encapsulating the link... Quote Link to comment https://forums.phpfreaks.com/topic/126858-solved-eliminate-links/#findComment-656168 Share on other sites More sharing options...
thebadbad Posted October 3, 2008 Share Posted October 3, 2008 If you want to keep the link text, but remove the actual anchor tag, this will work: preg_replace('~<a.*?>(.*?)</a>~is', '$1', $str); Or if you want to replace the whole link with the URL specified with the href attribute (i.e. a non-clickable link): preg_replace('~<a.*?href=[\'"]?(.*?)[\'"]?.*?>.*?</a>~is', '$1', $str); Quote Link to comment https://forums.phpfreaks.com/topic/126858-solved-eliminate-links/#findComment-656170 Share on other sites More sharing options...
AV1611 Posted October 3, 2008 Author Share Posted October 3, 2008 one last question: $str="this is a BIG word"; echo preg_replace('/\[size.*?\](.*?)\[/size\]' , '\\1' , $str); //this is a BIG word What did I do wrong? I just wanna drop the bbcode in this case... Quote Link to comment https://forums.phpfreaks.com/topic/126858-solved-eliminate-links/#findComment-656176 Share on other sites More sharing options...
thebadbad Posted October 3, 2008 Share Posted October 3, 2008 You forgot a closing pattern delimiter (and used one - the forward slash - unescaped in the regex). I think it's easier to use an odd character like ~: preg_replace('~\[size.*?\](.*?)\[/size\]~is' , '\\1' , $str); Quote Link to comment https://forums.phpfreaks.com/topic/126858-solved-eliminate-links/#findComment-656182 Share on other sites More sharing options...
thebadbad Posted October 3, 2008 Share Posted October 3, 2008 BTW, the i and s I added after the closing pattern delimiter are pattern modifiers. The i makes the search case insensitive, and the s makes the dot match new line characters also (in case there's a line break in the BB code). Quote Link to comment https://forums.phpfreaks.com/topic/126858-solved-eliminate-links/#findComment-656184 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.