gpt4 book ai didi

php - preg_replace 匹配但不替换

转载 作者:可可西里 更新时间:2023-10-31 23:56:12 27 4
gpt4 key购买 nike

在下面的代码中,我尝试获取一些文本并在其周围包裹一个 html anchor :

$d = "hello, there, how, are, you";
$d = preg_replace("/[a-zA-Z]+/", "<a href='?word=$1'>$1</a>", $d);

它正确地识别了每个单词并用替换文本替换了每个单词但是它没有将匹配的单词放入字符串中,它只是将其留空。我在 PHP5 上。我错过了什么?

提前致谢。

最佳答案

您忘记捕获匹配项:

$d = "hello, there, how, are, you";
$d = preg_replace("/([a-zA-Z]+)/", "<a href='?word=$1'>$1</a>", $d);
// ^ ^ you forgot these ;-)

关于php - preg_replace 匹配但不替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9035182/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com