Re: Regexp problem.... impossible ?
| From: | Marek Kilimajer | Date: | Fri, 11 Apr 2003 09:12:40 +0000 |
| Subject: | Re: Regexp problem.... impossible ? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-143466@lists.php.net to get a copy of this message | ||
Try perl compatible regex, it's ungreedy.
Antonio Navarro Navarro wrote:
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