ironik

msg:1245435 | 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

msg:1245436 | 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

msg:1245437 | 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

msg:1245438 | 11:10 pm on Feb 23, 2005 (gmt 0) |
Welcome to WebmasterWorld ironik
|
ironik

msg:1245439 | 11:41 pm on Feb 23, 2005 (gmt 0) |
Thanks ;) Been lurking for a while, thought it was time to sign up!
|
andrew_m

msg:1245440 | 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

msg:1245441 | 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

msg:1245442 | 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.
|
|