Jump to content

[SOLVED] string manipulation


mfindlay

Recommended Posts

Hi, In the code below I am trying to format an unordered list with php, I am using JavaScript to add tags to a textarea and then want to format the list in the php script...so:

 

<ul><li>List 1<br>List 2<br>List 3</li></ul>

//would become
<ul>
<li>list 1</li>
<li>list 2</li>
...
</ul>

 

I have been using lots of str_replace to strip esscape chars etc, which is why the code is so far formatted as above, below is what I have so far..

 

$add_data = "\"" . $explanation . "\"" . "," . "\"" . $question_title . "\"" . "," . "\"" . $question_stem . "\"\n";
$add_data = str_replace("\n" , "<BR>" , $add_data);
$add_data = str_replace("\r" , "" , $add_data);
$add_data = str_replace("<UL>" , "<UL><LI>" , $add_data);
$add_data = str_replace("<\UL>" , "<\LI><\UL>" , $add_data);
$add_data = str_replace("\"<BR>" , "\"\n" , $add_data);

 

any help apreciated....

 

Cheers!!

Link to comment
Share on other sites

Hello again...

 

with regard to above, I suppose I want to find the start (<ul><li>) and end (</li></ul>) of the portion of the string and replace all the occurrences of <br> with </li><li> (this part is easy), I'm just not sure how to find the start and end points??

 

again any help appreciated....

 

Cheers!

Link to comment
Share on other sites

sorry some code was emmitted....

 

with regard to above, I suppose I want to find the start (<ul><li>) and end (</li></ul>) of the portion of the string and replace all the occurrences of

<br>

with </li><li> (this part is easy), I'm just not sure how to find the start and end points??

 

again any help appreciated....

 

Cheers!

Link to comment
Share on other sites

Here's one method:

<?php
$str = '<ul><li>List 1<br>List 2<br>List 3</li></ul>';
$new_str = implode("</li>\n<li>",explode("<br>",$str));
$new_str = nl2br(str_replace(array("<ul>","</ul>"),array("<ul>\n","\n</ul>"),$new_str)) . "\n";
echo $new_str;
?>

 

Ken

Link to comment
Share on other sites

Thanks Ken, the list is just part of a string, so I guess the actual string could look more like...

 

<b>some text</b>
<br><br>
<ul><li>List 1<br>List 2<br>List 3</li></ul>
some more text...

 

so I need to search for the start and end of the list and just change the breaks in the middle

 

Thanks

Link to comment
Share on other sites

figured it out!! this is what I came up with...

 

for ($ul = 0; $ul < substr_count($add_data , "<UL><LI>"); $ul++)
{
	$find_ul_start = '<UL><LI>';
	$find_ul_end = '</LI></UL>';
	$ul_start = strpos($add_data, $find_ul_start);
	$ul_end = strpos($add_data, $find_ul_end);
	$length = $ul_end - $ul_start;
}

$new_string = substr($add_data,$ul_start,$length);

$new_string1 = str_replace("<BR>" , "</LI><LI>" , $new_string);
$add_data = str_replace($new_string , $new_string1 , $add_data);

 

Cheers  :)

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.