MyCode Posted July 18, 2010 Share Posted July 18, 2010 Hello, I am looking for a solution to remove one or multiple <br>'s from the start of a string. Example: I am loading a string from my mysql database and the content is: <br><br>This is my string It can be that it's just one <br>, but it could also be three or four <br>'s So what I am looking for is some way to get rid of the <br>'s but only at the beginning of the string as there will be some <br>'s inside the content which should stay. Any idea how to solve this? Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted July 18, 2010 Share Posted July 18, 2010 Remove <br />'s from start of string $str = '<br /><br /><br />hello world there<br /><br />bo hoo!'; $str = preg_replace('~^(\<br /\>)+~', '', $str); echo $str; Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted July 18, 2010 Share Posted July 18, 2010 I just quickly tested this, but it seems to work. Takes off any leading and trailing <br>, <br />, <br/>, <br >. etc. and leaves the rest of the string alone. $string = "<br /><br /><br /><br ><br/>string of<br><br /> words <br /><br>"; echo trim($string, '<br />'); // RETURNS string of<br><br /> words Quote Link to comment Share on other sites More sharing options...
MyCode Posted July 18, 2010 Author Share Posted July 18, 2010 Thanks for your quick answer(s). Everything seems to work wonderful now Thanks again Quote Link to comment Share on other sites More sharing options...
salathe Posted July 19, 2010 Share Posted July 19, 2010 trim($string, '<br />'); Just to make it clear, this code will remove more than just <br> tags from the start of a string. Given breakfast the result would be eakfast, given <b>robber</b> would result in obbe. Quote Link to comment 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.