RE: Why does this preg_replace function not work?

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

 



[snip]
"[" . $tags . "]"
[/snip]

This in the regex would need to be "\[" . $tags . "\]" as the square
brackets ([]) represent a character class in regular expressions, so must be
escaped to be matched literally

Also /'s should be escaped (\/)

There may be more mistakes but I thought I'd point this one out :)

HTH

Dan

-- 
Dan Parry
Senior Developer
Virtua Webtech Ltd
http://www.virtuawebtech.co.uk

-----Original Message-----
From: Dave M G [mailto:martin@xxxxxxxxxxxxx] 
Sent: 26 May 2006 10:26
To: php-general@xxxxxxxxxxxxx
Subject:  Why does this preg_replace function not work?

PHP List,

In the code below, I want to take the text within $content, and change 
every instance of [h3] into <h3>, and every instance of [/h3] into 
</h3>. And then do the same for [em], [/em], [strong], and so on.

However, this code does absolutely nothing to the text stored in content:

$tags = array ("h3", "em", "strong", "hr");
$content = preg_replace("[" . $tags . "]", "<" . $tags . ">", $content);
$content = preg_replace("[/" . $tags . "]", "</" . $tags . ">", $content);

Clearly I've either misunderstood the use of preg_replace(), or regular 
expressions, or arrays, despite having looked them up in the PHP online 
manual.

I also tried str_replace(), but predictably that did not help. As far as 
I understand it, it does not accept arrays.

What am I doing wrong in the above code?

And can the two preg_replace() commands be achieved in one line?

Thank you for any advice.

--
Dave M G

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

-- 
No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.1.392 / Virus Database: 268.7.0/346 - Release Date: 23/05/2006
 

-- 
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.1.392 / Virus Database: 268.7.0/346 - Release Date: 23/05/2006
 

-- 
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