4

I want to convert following text into list items

* Item 1
* Item 2

- item 1
- item 2

to

<ul>
  <li>Item 1</li>
  <li>Item 2</li>
</ul>

I have made following regex which is not good enough for this

$text = preg_replace('#(*\s[*.]\s*)#','<li>$0</li>', $text); 

but that does not work. I am not good at making RE.

I am making question more clear here.

A text may contain bullets or may not and I cant loop through the file as atno suggested.

Here are the samples

* HTML *SEO * Javascript * PHP

- HTML
- SEO
-Javascript
-PHP

-HTML - SEO -Javascript -PHP
6
  • no text from database. Its user entered text. Commented May 29, 2011 at 23:46
  • are you trying to create a markdown system like stackoverflow and github use ? Commented May 29, 2011 at 23:49
  • 4
    Why not support a complete markup language, like Markdown, and let a parser do the job for you? Commented May 29, 2011 at 23:49
  • @jason show us a bit more code, how does the text you're retrieving from the database looks like Commented May 29, 2011 at 23:50
  • @atno: That's not really relevant. All the information you need to know is in the question. Commented May 29, 2011 at 23:51

3 Answers 3

2

So maybe something along the lines of:

<?PHP
$text = <<<Text
* HTML *SEO * Javascript * PHP

- HTML
- SEO
-Javascript
-PHP

-HTML - SEO -Javascript -PHP
Text;

$text = preg_replace('/(\*|-)\s*([\S]+)\s*/',"<li>$2</li>\n",$text);

print $text;
?>

which gives an output of:

<li>HTML</li>
<li>SEO</li>
<li>Javascript</li>
<li>PHP</li>
<li>HTML</li>
<li>SEO</li>
<li>Javascript</li>
<li>PHP</li>
<li>HTML</li>
<li>SEO</li>
<li>Javascript</li>
<li>PHP</li>
Sign up to request clarification or add additional context in comments.

4 Comments

+1 for you, i was about to paste my solution but i wasn't using preg_replace as i'm not familiar with
we still need to sort out <ul></ul>
@jason, where do you want the <ul>'s, I was presuming they'd just be at the start and end!
these list items can be anywhere in the text or it may not be at all
1

A little nasty to do with regular expressions, but here you go:

<?php
$text = <<<TEXT
* HTML *SEO * Javascript * PHP

- HTML
- SEO
-Javascript
-PHP

-HTML - SEO -Javascript -PHP
TEXT;

$text = preg_replace_callback('`([*-]\s*([^*-\r\n]+)(\r?\n)?)+`', function($m) {
    $str = '<ul>';
    $str .= preg_replace('`[*-]\s*([^*-\r\n]+)\s*`', '<li>$1</li>', $m[0]);
    $str .= '</ul>';
    return $str;
}, $text);

echo $text;

I get this as output:

*snip* clarification changes output

2 Comments

This is the best solution, however I am still trying to make it perfect. I am trying to convert only those list items which are more than one for example *a *b - c *d - e * f * g in this case a and b will be converted into list items and f and g only. I have written following RE #(*\s*([^*\r\n]+)([*\r\n*]))# This works fine except it does not work for last item say g.
oh, I was assuming you didn't want to mix list types. I deliberately did it this way so they'd be converted to separate lists, and blank lines in between would also end the list. should be able to replace the \* with [*-] instead in the first block (it's in there twice) and then just scrap the second part. here..i'll edit, but cant test here
0

ok this is the best i can come up with but it solve part of the problem, maybe someone else can find a better

// first i remove the spaces after the hyphen, like in '- SEO' to have some consistency 
$str = str_replace ('- ','-', $str); 

// then i look for hyphen-word-new line  and replace it with the format i want.
$list = preg_replace('#\-(.*)\n#',"<li>$1</li>\n", $str);

Obviously this will not be completely correct because you still need the <ul> tag. so good luck!

Comments

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.