Re: Why does this preg_replace function not work?

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

 



Dave M G wrote:

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

First of all, why the hell are you using preg_* functions for this? You're feeding static content to it, no modifiers *at all* (not even case-insensitivity). I recommend you go back to str_replace() as that is what you need. You'd also be wise to read up on arrays and regular expressions (a lot).

preg_replace() uses regular-expressions. Regular expressions require (in php) 2 delimiters, one at the start of the expression and one at the end, followed by optional modifiers/flags. Eg:
/regexpGoesHere/i
this would match "regexpGoesHere" and be case-insensitive.
You don't use delimiters (first problem).
Second problem with your code is that you're assuming that [, ], < and > are not meta-characters. Unfortunately, [ and ] ARE meta-characters. This means that when you would pass it "[h3]" it would see that as "any character which is an 'h' or '3' is a valid candidate for this expression". You would either need to escape it so it becomes \[h3\] which would mean "any string looking like '[h3]' is a valid candidate".

Right, well, first let's go and fix the mess you've made of your arrays.
Here's a lesson for you:
Say you have
$array = array('a','b','c');
print($array);
print($array);

What do you expect to see?
a
b
?

Because looking at your code it seems like you're expecting something very strange. The thing you'll see is:
Array
Array

Your correct version would be to either loop over it using a construct such as foreach(), while() or the like, OR use the special case of preg_replace and str_replace functions, which may also take 2 arrays as their parameters. Remember though, you CAN NOT MIX ARRAYS WITH STRINGS just like that.

So, a more correct version for you would be (using str_replace because i's faster and easier and more appropriate): $tagsOld = array ("[h3]", "[em]", "[strong]", "[hr]","[/h3]", "[/em]", "[/strong]", "[/hr]"); $tagsNew = array ("<h3>", "<em>", "<strong>", "<hr>","</h3>", "</em>", "</strong>", "</hr>");
$content = str_replace($tagsOld, $tagsNew, $content);

What I've done here is made an array with what is to be replaced and a second one with what it is to be replaced with. Internally, str_replace goes over the whole list of $tagsOld and replaces each value with the corresponding value from $tagsNew (based on position in the array, meansing the 2nd value from $tagsOld will be replaced with the 2nd value from $tagsNew).

hope you understand what you did (wrong) now,
- tul

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