Jump to content

php split text in a line into pieces


sokha

Recommended Posts

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

<?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>";
?>

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>

 

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>";
?>

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.