Regexp problem.... impossible ?
| From: | Antonio Navarro Navarro | Date: | Fri, 11 Apr 2003 08:46:21 +0000 |
| Subject: | Regexp problem.... impossible ? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-143464@lists.php.net to get a copy of this message | ||
Hi all !
I'm writing a small Wiki-like environment in Php. One of the formating
functions I want to implemet is the following :
- Any text surrounded by ++ symbols must appear in bold.
Ok, so here is the code :
$string = 'This is a test of ++ bold text ++ in Php.';
$string = ereg_replace("(\+{2})(.*)(\+{2})",'<b>\\2</b>', $string);
after this, $string contains : 'This is a test of <b>bold text</b> in Php.',
so it seems to work.
Second test :
$string = 'This is a test of ++ bold text ++ in ++ Php ++.';
This fails, , $string contains : 'This is a test of <b>bold text ++ in ++
Php</b>.'.
Well, the correct aproach is change the middle part of the pattern (.*) for
a pattern for not allowing the + char :
$string = ereg_replace("(\+{2})([^+]*)(\+{2})",'<b>\\2</b>',
$string);
This works, $string contains : 'This is a test of <b>bold text </b> in
<b>Php</b>.'.
Ok, but If I want to add a '+' simbol in the text....
$string = 'This is a test of ++ bold + text ++ in ++ Php ++.';
$string = ereg_replace("(\+{2})([^+]*)(\+{2})",'<b>\\2</b>',
$string);
failure : 'This is a test of ++ bold + text <b>in</b> Php ++.
Ok, lets check for two + symbols :
$string = 'This is a test of ++ bold + text ++ in ++ Php ++.';
$string = ereg_replace("(\+{2})([^++]*)(\+{2})",'<b>\\2</b>',
$string);
failure : 'This is a test of ++ bold + text <b>in</b> Php ++.
Any idea about how to solve this problem ?
Regards
Antonio Navarro Navarro
hostmaster@bemarnet.es