0

假设我有以下href:

.... href="http://localhost/centboox/usedtextbooks/desc/30" ....

我想通过所有这些并在关闭 href 的双引号之前添加* * :

... href="http://localhost/webname/pagename/desc/30*******" ...

星号代表一些输入,例如 ?q=all&a=search_text

我如何通过 PHP 中的 preg_replace 做到这一点。

谢谢

4

4 回答 4

3
$result = preg_replace('/href="[^"]*/', '\0********', $subject);
于 2012-10-03T04:55:28.510 回答
0

如果您要始终将其连接到字符串的末尾,有什么理由不能仅仅连接?

// Assuming you're array is already filled out...
$somethingToAppend = "?q=all&a=search_text"

foreach($hrefs as $key => $value) {
    $hrefs[$key] = $value + $somethingToAppend;
}

干杯

于 2012-10-03T04:56:43.640 回答
0

这是一个简单的例子:

$href = <<<EOHTML
<a href="test.html?value=one">LINK</a>
EOHTML;

$href = preg_replace("/href=\"([^\"]*)\"/",
   "href=\"$1**\"", $href);

echo $href;

在我正在寻找的正则表达式模式中href="(something)",我将其替换为href="(something)**".

于 2012-10-03T04:58:08.087 回答
0

这是 preg_replace 的示例,它可能对您有用。检查下面的链接

http://php.net/manual/en/function.preg-replace.php

于 2012-10-03T04:59:51.127 回答