Fabis94 Posted April 17, 2009 Share Posted April 17, 2009 How do i trim out a link from a string? Each time the link could be different (for example once it might be <a href="www.somesite.com">AAA</a> and another time it might be <a href="www.othersite.net">BBB</a>). How do i make it delete the whole link? Quote Link to comment https://forums.phpfreaks.com/topic/154541-solved-trimming-a-string/ Share on other sites More sharing options...
premiso Posted April 17, 2009 Share Posted April 17, 2009 Do you want to keep the BBB or AAA part? If so strip_tags may be what you are after. Quote Link to comment https://forums.phpfreaks.com/topic/154541-solved-trimming-a-string/#findComment-812573 Share on other sites More sharing options...
Fabis94 Posted April 17, 2009 Author Share Posted April 17, 2009 I want to remove the whole link. Quote Link to comment https://forums.phpfreaks.com/topic/154541-solved-trimming-a-string/#findComment-812585 Share on other sites More sharing options...
The Little Guy Posted April 17, 2009 Share Posted April 17, 2009 This worked for me: <?php function tagStripReplace($content){ return preg_replace("~<a(w*[^>])*>.+?</a>~", '', $content); } echo tagStripReplace("<p>I like pizza</p><a href=\"aasdf\">Link</a> this is a <b>link</b>"); ?> its a modification of this code: http://beta.phpsnips.com/snippet.php?id=38 Quote Link to comment https://forums.phpfreaks.com/topic/154541-solved-trimming-a-string/#findComment-812622 Share on other sites More sharing options...
Fabis94 Posted April 18, 2009 Author Share Posted April 18, 2009 strip_tags() works well, but it doesn't completely remove the <div> tags. I have places like <div class="something">Sometext</div> and it doesn't remove them. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/154541-solved-trimming-a-string/#findComment-813017 Share on other sites More sharing options...
thebadbad Posted April 18, 2009 Share Posted April 18, 2009 It should. What does your script look like? Quote Link to comment https://forums.phpfreaks.com/topic/154541-solved-trimming-a-string/#findComment-813019 Share on other sites More sharing options...
Fabis94 Posted April 18, 2009 Author Share Posted April 18, 2009 Well it does remove the <DIV> tag, but not the text in it (the Sometext part). And the function that The Little Guy posted only removes links. Quote Link to comment https://forums.phpfreaks.com/topic/154541-solved-trimming-a-string/#findComment-813020 Share on other sites More sharing options...
thebadbad Posted April 18, 2009 Share Posted April 18, 2009 Ah, if you want to remove both tags and the contains, try this function from the comments in the manual. Edit: The forum messed it up, so check it via the link. Quote Link to comment https://forums.phpfreaks.com/topic/154541-solved-trimming-a-string/#findComment-813045 Share on other sites More sharing options...
Fabis94 Posted April 18, 2009 Author Share Posted April 18, 2009 Thanks man, that's just what i needed Quote Link to comment https://forums.phpfreaks.com/topic/154541-solved-trimming-a-string/#findComment-813078 Share on other sites More sharing options...
The Little Guy Posted April 18, 2009 Share Posted April 18, 2009 I know the topic is solved and all (also didn't realize you wanted to remove divs too) but... I modified my code to remove divs as well. It is shorter that the above link... If you decided to do this (or even read this) let me know how it works for you! <?php function tagStripReplace($content){ return preg_replace("~<(a|div)(w*[^>])*>.+?</(a|div)>~", '', $content); } echo tagStripReplace("<div>I like pizza</div><a href=\"aasdf\">Link</a> this is a <div class=\"div\"><b>link</b></div>"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/154541-solved-trimming-a-string/#findComment-813288 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.