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, Link to comment https://forums.phpfreaks.com/topic/192359-php-split-text-in-a-line-into-pieces/ 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 Link to comment https://forums.phpfreaks.com/topic/192359-php-split-text-in-a-line-into-pieces/#findComment-1013611 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. Link to comment https://forums.phpfreaks.com/topic/192359-php-split-text-in-a-line-into-pieces/#findComment-1013612 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>"; ?> Link to comment https://forums.phpfreaks.com/topic/192359-php-split-text-in-a-line-into-pieces/#findComment-1013614 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> Link to comment https://forums.phpfreaks.com/topic/192359-php-split-text-in-a-line-into-pieces/#findComment-1013619 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>"; ?> Link to comment https://forums.phpfreaks.com/topic/192359-php-split-text-in-a-line-into-pieces/#findComment-1013623 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. Link to comment https://forums.phpfreaks.com/topic/192359-php-split-text-in-a-line-into-pieces/#findComment-1013628 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.