Re: Why does this preg_replace function not work?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 26/05/06, Dave M G <martin@xxxxxxxxxxxxx> 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);
?>

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux