August 8th, 2012, 11:16 AM
Finding and replacing one meta tag
I am trying to replace the keyword meta tag:
with some other keywords. I am using preg_replace in PHP, but can't seem to get it to work. :/
<meta name="keywords" content="my keywords, x, y, z" />
This is how my code currently looks:
can someone help me here please?
preg_replace('/<meta name=\"keywords\" content=\"(.*)\" \/>/i','<meta name="keywords" name="my new keywords" />', $htmlOutput);
thanks and kind regards
August 8th, 2012, 11:21 AM
.* is greedy so will match everything up to the last '" />' sequence in your string, not the next one. A quick fix would be to use .*? instead. Better yet is to avoid dot-star altogether. Though Perl-focused, this article is worth a read.
August 8th, 2012, 11:24 AM
Thanks for the link. This helps getting more insight in regular expressions, which I am really not good at.
After trying around for an hour I decided to post this problem here. A few minutes later I find the error in a misspelled variable. The regular expression was working all the way...
Sry for posting!