Escape captured expression with PHP preg_replace
I am struggling with preg_replace
and am really hoping someone can spot my error. Regular expressions seem to be my Achilles heel.
I'm trying to replace strings like {{123.jpg}}
with <img src="http://mydomain/some/path/123.jpg"></img>
I searched similar questions and found preg_quote
, but I don't believe I have any regex characters in the non-regex part of my replacement string, so I'm at a loss.
Here's what I'm trying:
function replace($article_id,$text){
$pattern = '/\{\{(.*?)\}\}/';
$replacement = '<img src="'.URL_CONSTANT.'articles/'.$article_id.'/'.'${1}'.'"></img>';
return preg_replace($pattern, $replacement, $text);
}
I can confirm that the pattern is matching, but it's not replacing properly. I think开发者_如何学运维 the issue may relate to the final /
before the ${1}
replacement, but I don't see why as I don't believe it's a special regex character.
Any help is appreciated!
EDIT: I discovered the issue was unrelated to my expression. Thanks to everyone who assisted in verifying that my expression was properly formed!
Thank you to all who helped! I discovered the issue wasn't the regular expression, but rather was the "scayt" autocorrect plugin for CKEditor. It was turning the {{124.jpg}} shortcut tags into spans because it detected them as misspelled words. I wrote another regular expression which removed these spans and replaced them with the original {{abc.def}} format. This seems to have solved the problem.
精彩评论