homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

Regular expression problem (PERL Syntax)
Parse list and convert to HTML format

 11:15 am on Feb 22, 2005 (gmt 0)


I'm having a tough problem that I want to solve with regular expressions. I'm pretty sure that it is possible for a regex to parse this thing, but since I only know the basic stuff, Im pretty much lost here.

I want to parse a string like this:

#list item1
#list item2
#list item3

And this is what I want the regex to return:

<li>list item1</li>
<li>list item2</li>
<li>list item3</li>

Basically, I want to parse my listing and return a valid HTML unordered list. The regex has to find start end end tags (##) and then parse the inner part to find strings like #text\n where \n is a line break.

Can anyone help me with this?

Thanks in advance!



 2:29 am on Feb 23, 2005 (gmt 0)

This function will work for one list per text call:

function convert_to_list($text)
$pattern1 = "/(\#\#\n)((\#{1})[\w?\s?\W?\S?]+\n)*(\#\#\n)/";
$replace1 = "<ul>$2</ul>";

$list = preg_replace($pattern1, $replace1, $text);

$pattern2 = "/(\#{1})(\w{1,}.+)?\n/";
$replace2 = "<li>\\2</li>";

$text = preg_replace($pattern2, $replace2, $list);
return $text;

I haven't been able to get it working for multiple lists in the same piece of text... perhaps someone here is able to tweak the regex a little so it will do multiple replacements successfully?

I'm still discovering regex's myself, so it's probably not that efficient. :(


 7:42 am on Feb 23, 2005 (gmt 0)

Thank you!

I don't understand the regex completely, but I know what it does ;)

It would be great if anyone could help getting this regex work with multiple lists in a text string.



 9:18 pm on Feb 23, 2005 (gmt 0)

If you want to learn some more about regex's heres a couple of links. Handy to know, especially when filtering and validating content:

Sample regex's (regex library)

In depth Regex tutorial

O'reilly regex pocket reference (PDF)

More regex references (this is probably the best one for beginners)


 11:10 pm on Feb 23, 2005 (gmt 0)

Welcome to WebmasterWorld ironik


 11:41 pm on Feb 23, 2005 (gmt 0)

Thanks ;)

Been lurking for a while, thought it was time to sign up!


 11:46 pm on Feb 23, 2005 (gmt 0)

You made me curious if that's possible as a single regex. This seems to work:

#!/usr/bin/perl -w
#list item1
#list item2
#list item3
another list
#another 1
#another 2

{ local $prefix=$1 . '<UL>' . $2;
local $suffix='</UL>' . $4;
(local $lines=$3)=~s/^#/<LI>/mg;
$prefix . $lines . $suffix;

print $a;

As a bonus you get 'gsex' (great sex) as a regex qualifier :)


 12:51 pm on Feb 24, 2005 (gmt 0)

@andrew_m: Thanks for your help! I honestly have no idea what your regex is doing for most of the time.

I tried to run it as a PERL script, but it didnt work. The error was: Can't find string terminator "EOT" anywhere before EOF at ./perltest line 2.

How can I get this working inside PHP's preg_replace() function?



 9:50 pm on Feb 24, 2005 (gmt 0)

I have no idea, sorry, don't know a thing about php. I thought you asked about perl and it does work fine in perl -- it's cut-n-paste from the actual script that I ran to test it.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved