开发者

strtolower inside preg_replace - possible?

开发者 https://www.devze.com 2022-12-22 05:11 出处:网络
Is it possible to use strtolower in the substitution part of preg_replace? This isn’t workin开发者_如何学编程g:

Is it possible to use strtolower in the substitution part of preg_replace?

This isn’t workin开发者_如何学编程g:

preg_replace('/(http:\/\/)?(www\.)?([a-zA-Z0-9\-_\.]+\.(com|co\.uk|org|tv|biz)(\/[a-zA-Z0-9\-\._\?&=#\+;]+)*)/i', '<a href="http://www.'.strtolower('$3').'" target="_blank">'.strtolower('$3').'</a>', $d);


It is possible, yes. Have a look at the e modifier (Example #4):

preg_replace('/(http:\/\/)?(www\.)?([a-zA-Z0-9\-_\.]+\.(com|co\.uk|org|tv|biz)(\/[a-zA-Z0-9\-\._\?&=#\+;]+)*)/ie', "'<a href=\"http://www.'.strtolower('$3').'\" target=\"_blank\">'.strtolower('$3').'</a>'", $d);

(Untested, the number of escaping backslashes may be wrong.)


I favor using preg_replace_callback() over using the e(eval) modifier. I feel the code is cleaner, and has less room for error.

0

精彩评论

暂无评论...
验证码 换一张
取 消