使用preg_replace我如何插入撇号和引号


Using preg_replace how can I inlude apostrophes and quotes

我需要更正某些 HTML 中的链接,我可以使用preg_replace但对如何使用引号 (") 和撇号 (') 感到困惑,例如,替换...

href=http://
href="http://
href='http://
href=/
href="/
href='/

如果没有包括引号,它看起来像...

$pattern = "href=http://";
$replacement = "href=http://example.com/test.php?go=http://";
$new = str_ireplace($pattern, $replacement, $html);

但是在这种情况下如何编写preg_replace代码?

$pattern = "href=http://'";
$replacement = "href=http://example.com/test.php?go=http://";

此外,通过$pattern的优先循环将是理想的。

$pattern = "/href=[''"]{0,1}('/|http:'/'/)/";
$replacement = "http://example.com/test.php?go=''1";
$new = preg_replace($pattern, $replacement, $html);