sokha Posted February 17, 2010 Share Posted February 17, 2010 I have a problem with a small php code. I have a string variable: $string = "word1*<p>description1...</p>word2*<p>description2.....</p>word3*<p>description3......</p>word4*<p>description4</p>"; I would like to split this string to get output: word1*description1... word2*description2..... word3*description3...... word4*description4 Any help would be great. Thanks, Quote Link to comment Share on other sites More sharing options...
Deoctor Posted February 17, 2010 Share Posted February 17, 2010 Have u checked this function explode.. http://php.net/manual/en/function.explode.php Quote Link to comment Share on other sites More sharing options...
sokha Posted February 17, 2010 Author Share Posted February 17, 2010 Have u checked this function explode.. http://php.net/manual/en/function.explode.php Yes, I have checked it. I could not figure out how to split it yet. I also tred to use with help of substr function but I always got incorrect result. Quote Link to comment Share on other sites More sharing options...
Deoctor Posted February 17, 2010 Share Posted February 17, 2010 <?php $string = "word1*<p>description1...</p>word2*<p>description2.....</p>word3*<p>description3......</p>word4*<p>description4</p>"; $piece=explode("</p>", $string); echo $piece[0]."<br>"; echo $piece[1]."<br>"; echo $piece[2]."<br>"; echo $piece[3]."<br>"; ?> Quote Link to comment Share on other sites More sharing options...
sokha Posted February 17, 2010 Author Share Posted February 17, 2010 Thanks for the answer. I just found out some lines contain more than one <p></p>. For example: $string = "word1*<p>description1......</p>word2*<p>description2.........</p><p>some text</p>word3*<p>description3... .....</p><p>some text</p><p>some text</p>word4*<p>description4.. </p><p>some text</p>"; And I would like to have the result similar to above result. word1*<p>description1......</p> word2*<p>description2.........</p><p>some text</p> word3*<p>description3... .....</p><p>some text</p><p>some text</p> word4*<p>description4.. </p><p>some text</p> Quote Link to comment Share on other sites More sharing options...
Deoctor Posted February 17, 2010 Share Posted February 17, 2010 try this. I think this one would give ur result.. <?php $string = "word1*<p>description1...</p>word2*<p>description2.....</p>word3*<p>description3......</p>word4*<p>description4</p>"; $piece=explode("</p>", $string); echo htmlspecialchars($piece[0])."<br>"; echo htmlspecialchars($piece[1])."<br>"; echo htmlspecialchars($piece[2])."<br>"; echo htmlspecialchars($piece[3])."<br>"; ?> Quote Link to comment Share on other sites More sharing options...
sokha Posted February 17, 2010 Author Share Posted February 17, 2010 The sample string value is (new): $string = "word1*<p>description1......</p>word2*<p>description2.........</p><p>some text</p>word3*<p>description3... .....</p><p>some text</p><p>some text</p>word4*<p>description4.. </p><p>some text</p>"; Some part has more than one </p> in between like this piece: word3*<p>description3... .....</p><p>some text</p><p>some text</p> So if we just only use explode by </b>, it is breaking that line too. 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.