Re: Why does this preg_replace function not work?
| From: | Robin Vickery | Date: | Fri, 26 May 2006 10:59:49 +0000 |
| Subject: | Re: Why does this preg_replace function not work? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-236939@lists.php.net to get a copy of this message | ||
On 26/05/06, Dave M G <martin@autotelic.com> wrote:
I also tried str_replace(), but predictably that did not help. As far as I understand it, it does not accept arrays.It does, and you can do it with str_replace.
What am I doing wrong in the above code? And can the two preg_replace() commands be achieved in one line?They can, however you need to build the pattern properly. <?php function to_html($content, $tags) { $regexp = '#\[(/?(' . join('|',array_map('preg_quote', $tags)) . '))\]#'; return preg_replace($regexp, '<$1>', $content); } $tags = array ("h3", "em", "strong", "hr"); $content = '[em] this [/em] is converted and [ignore] this [/ignore] is not.'; $content = to_html($content, $tags); ?>