且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PHP使用str_replace和preg_replace

更新时间:2021-07-03 17:18:04

您正在将str_replace()调用放在preg_replace()调用的输出模式中.这意味着preg_replace()会将其视为文字文本.

You're putting the str_replace() call in the output pattern for the preg_replace() call. That means preg_replace() is treating it as literal text.

您想要的是这样的

$imgtag = preg_replace(match, replacement, $a);
$imgtag = str_replace('-m','-l',$imgtag);

但是,我认为,如果您更改替换操作的顺序,调试这些内容会更安全,更轻松,例如:

But, in my opinion it would be safer and easier to debug this stuff if you changed the order of your replacement operations, something like this:

foreach ($path in explode(",", $a)) {
  $path = str_replace('-m','-l',$path);
  $imgtag= sprintf ('<img class="gallery" src="%s">', $path);
  /* do something with the $imgtag */
}

这样一来,您不必吹口哨调制解调器:-)即可对该正则表达式进行编程.

That way you don't have to whistle into your modem :-) to program that regexp.