1

I have a table column that contains a list of items separated by "br" tag(no new lines).

I fetch this data and convert br tags to new lines like as defined below.

$nlformat = str_replace("<br>","\n",$mem['mem_services']);

I'm also writing a file in this php script that will turn this list into an html list with "li" tags. This is easy, BUT if the list has two consecutive new lines "\n\n" I want to end the first list and create a second list like so...

<ul>
<li>item</li>
<li>item</li>
<li>item</li>
</ul>
<ul>
<li>item</li>
<li>item</li>
<li>item</li>
</ul>

I'm having an issue identifying 2 consecutive new lines in the following variable. Below is just one of many examples from other answered questions on this site. None of them work

$newul = preg_replace("/\r\n\r\n|\n\n|\r\r/", "</li></ul><ul><li>", $nlformat);

Does anyone know why. Maybe it has something to do with the $nlformat variable.

With the current example I get no change to $nlformat i.e.

item
item
item

item
item
item
5
  • What do you get instead of the desired result? Commented Sep 17, 2016 at 1:14
  • It works for me: ideone.com/5jj6lx Commented Sep 17, 2016 at 1:19
  • @Barmar - Interesting that it works on the testing site you referred me to as I tried the code you used and upload to my server and it does not work. Commented Sep 17, 2016 at 1:58
  • Maybe it's a confusion nix/windows EOL, try to update the preg_replace accordingly Commented Sep 17, 2016 at 6:14
  • It looks like your regexp handles all EOL formats, so it should work. Commented Sep 17, 2016 at 23:59

1 Answer 1

2

My approach to this question is, I have a string that has \n char in between. I will first build an formatted array out of it and then build html out of it. So that you can use section of code that is related to your problem

Please try this way.

$nlFormat="item\nitem\nitem\n\nitem\nitem\nitem";
$tmp_arr1= explode("\n\n",$nlFormat);
$final_arr=array();
// now $tmp_arr1 has n number of sections;
foreach($tmp_arr1 as $section){
    $final_arr[]= explode("\n",$section);
}

//var_dump($final_arr);

now lets generate html

$html="";
foreach($final_arr as $section){
    $html.="<ul><li>";
    $html.=implode("</li><li>",$section);
    $html.="</li></ul>";
}
echo $html;
Sign up to request clarification or add additional context in comments.

4 Comments

I see some potential with your suggestion as i am able to produce two arrays now i just need to play with this to see if i can produce two html lists.
I have not tried running this code, let me know if this helps you
Ahhhh, thanks for your help. So much appreciated as I don,t want to mention how many hours I have spent with preg_replace (). So I got rid of the var_dump and added a semi-colon at the end of the $finalarray[] vaiable. Works like a charm!
Also want to mention this is a very innovative approach. Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.