preg_replace
expects it’s regex to be surrounded by a character. Usually that’s a slash, like this:
'/.*/'
When you do this:
preg_replace('%%PROPOSAL_LINK_URL%%', $some_text, $replacement);
preg_replace
thinks the first two %
are your surrounding characters, and fails because of an unrecognized modifier. This is easy to test:
$ php -a
Interactive shell
php > echo preg_replace('%%PROPOSAL_LINK_URL%%', '%%PROPOSAL_LINK_URL%%', 'here'), PHP_EOL;
PHP Warning: preg_replace(): Unknown modifier 'P' in php shell code on line 1
PHP Stack trace:
PHP 1. {main}() php shell code:0
PHP 2. preg_replace() php shell code:1
Warning: preg_replace(): Unknown modifier 'P' in php shell code on line 1
Call Stack:
28.9460 235264 1. {main}() php shell code:0
28.9460 236016 2. preg_replace() php shell code:1
php >
You have two options:
Change your patterns to something like this: /%%PROPOSAL_LINK_URL%%/
and /%%PROPOSAL_LINK%%/
$ php -a
Interactive shell
php > echo preg_replace('/%%PROPOSAL_LINK_URL%%/', '%%PROPOSAL_LINK_URL%%', 'here'), PHP_EOL;
here
Forget all about preg_replace
because you don’t need it. Use str_replace
or str_ireplace
instead.
<?php
str_replace('%%PROPOSAL_LINK_URL%%', $proposal_link_url, $email_content);
str_ireplace('%%PROPOSAL_LINK%%', $proposal_link, $email_content);
Be aware that str_replace
and it’s brethren are not safe for use on strings that contain multi-byte characters (eg. UTF-8) by default. You can change this.
NOTE: you’ve used $link_pattern
twice in your code examples. I think you mean the second one to be $url_pattern
.
EDIT: it appears there’s not a mb_*
replacement for str_replace
or str_ireplace
this comment on php.net sheds some light on the issue. str_replace
should work on multibyte strings, providing the encoding of the needle and haystack are the same.