homepage Welcome to WebmasterWorld Guest from 54.237.78.165
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
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
Garfield

10+ Year Member



 
Msg#: 7049 posted 11:15 am on Feb 22, 2005 (gmt 0)

Hi,

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:

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

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!

 

ironik

5+ Year Member



 
Msg#: 7049 posted 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. :(

Garfield

10+ Year Member



 
Msg#: 7049 posted 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.

Thanks!

ironik

5+ Year Member



 
Msg#: 7049 posted 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)
[regexlib.com...]

In depth Regex tutorial
[regular-expressions.info...]

O'reilly regex pocket reference (PDF)
[oreilly.com...]

More regex references (this is probably the best one for beginners)
[phpbuilder.com...]

jatar_k

WebmasterWorld Administrator jatar_k us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 7049 posted 11:10 pm on Feb 23, 2005 (gmt 0)

Welcome to WebmasterWorld ironik

ironik

5+ Year Member



 
Msg#: 7049 posted 11:41 pm on Feb 23, 2005 (gmt 0)

Thanks ;)

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

andrew_m

10+ Year Member



 
Msg#: 7049 posted 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
$a=<<EOT;
##
#list item1
#list item2
#list item3
##
another list
##
#another 1
#another 2
##
EOT

$a=~s{(^\r\n)
\#\#([\s\r\n]+)
((?:\#.*?[\r\n])+?)
\#\#([\r\n\s]+)
}
{ local $prefix=$1 . '<UL>' . $2;
local $suffix='</UL>' . $4;
(local $lines=$3)=~s/^#/<LI>/mg;
$prefix . $lines . $suffix;
}gsex;

print $a;

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

Garfield

10+ Year Member



 
Msg#: 7049 posted 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?

Thanks!

andrew_m

10+ Year Member



 
Msg#: 7049 posted 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