treeleaf20 Posted June 28, 2010 Share Posted June 28, 2010 All, I have the following text: <bold>Our Services Include:<endbold> <bullet>Construction<endbullet><bullet>Landscaping<endbullet> Then I have the following PHP to replace the contents: $qrytxt = "Select * from texts where text_id='2'"; $resulttxt = mysql_query($qrytxt); $resultsettxt = mysql_fetch_array($resulttxt); $newtxt = $resultsettxt['text']; $newtxt = str_replace("<endbullet><bullet>", "</li><li>", $newtxt); $newtxt = str_replace("<bullet>", "<li>", $newtxt); $newtxt = str_replace("<endbullet>", "</li><br>", $newtxt); $newtxt = str_replace("<bold>", "<strong>", $newtxt); $newtxt = str_replace("<endbold>", "</strong>", $newtxt); echo nl2br($newtxt); My problem is that if it is the first <bullet> then I want it to replace to <ul><li> instead of just a <li> and also if it's the last <endbullet> then I want it to replace it with </li></ul> Any ideas on how to do that? Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/206122-help-with-string-replacing/ Share on other sites More sharing options...
litebearer Posted June 28, 2010 Share Posted June 28, 2010 Perhaps try... $newtxt = "<UL><br>" . $resultsettxt['text']; $newtxt = str_replace("<endbullet>", "</li><br>", $newtxt); $newtxt = str_replace("<bullet>", "<li>", $newtxt); $newtxt = str_replace("<bold>", "<strong>", $newtxt); $newtxt = str_replace("<endbold>", "</strong>", $newtxt); $newtxt = $newtxt . "</ul><br>"; echo $newtxt; Quote Link to comment https://forums.phpfreaks.com/topic/206122-help-with-string-replacing/#findComment-1078481 Share on other sites More sharing options...
Zagga Posted June 28, 2010 Share Posted June 28, 2010 Hi treeleaf20, You can use the strstr() function to find the first occurrence of a string (inside a string), and use strrchr() to find the last occurrence. Use these functions before you do the rest of the replacing. Hope this helps. Zagga Quote Link to comment https://forums.phpfreaks.com/topic/206122-help-with-string-replacing/#findComment-1078482 Share on other sites More sharing options...
sasa Posted June 29, 2010 Share Posted June 29, 2010 try <?php $test = '<bold>Our Services Include:<endbold> <bullet>Construction<endbullet><bullet>Landscaping<endbullet>'; $test = preg_replace('/<bold>(.*?)<endbold>/s', '<strong>$1</strong>', $test); $test = preg_replace('/<bullet>(.*?)<endbullet>/s', '<li>$1</li>', $test); $test = preg_replace('/<li>.*<\/li>/s', '<ul>$0<br /></ul>', $test); echo $test; ?> Quote Link to comment https://forums.phpfreaks.com/topic/206122-help-with-string-replacing/#findComment-1078579 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.