$ string = preg_replace(&#; name =([a-zA-Z0-9 .-] +)*]#",''。" $ 1",$ string);

部分脚本不起作用:

str_replace(' ', '-', "$1")

我需要更换“ "用“ - ”, 我还在main preg_replace str_ireplace 中尝试 preg_replace

但这仍然无效

有帮助吗?

解决方案

预先评估更换,而不是每次更换。但您可以使用 e 正则表达式中的修饰符

$string = preg_replace("#\[name=([a-zA-Z0-9 .-]+)*]#e", '"<td><a href=\"$front_page/".str_replace(" ", "-", "$1")."\">$1</a></td>"', $string);

或使用 preg_replace_callback

function callbackFunction($match) {
    global $front_page;
    return '<td><a href="'.$front_page.'/'.str_replace(" ", "-", $match[1]).'">'.$match[1].'</a></td>';
}
$string = preg_replace_callback("#\[name=([a-zA-Z0-9 .-]+)*]#", 'callbackFunction', $string);

其他提示

我想你必须分两步完成,因为 $ 1 不能在 str_replace()中使用。 $ 1 并不真正作为变量存在,它只是替换字符串中的占位符。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top