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

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.