[英](preg_replace) regex replace all & in <a href=“”>
I somehow can't get this to work: I have a simple string, for example: 我不知道怎么办这个:我有一个简单的字符串,例如:
<p>Foo & Bar</p> // <-- this should still be &
<a href="http://test.com/?php=true&test=test&p=p"> // <- This string should only be affected and be changed to &
<div> Yes ü No</div> // <-- This should still be ü
<a href="http://mycoolpage.com/?page=1&fun=true&foo=bar&yes=no">
Now I want to replace all the &
现在我要替换所有&
with only &
with preg_replace
and I tried to create a regex for this, but somehow I can't get it to work. 只有&
与preg_replace
,我试图为此创建一个正则表达式,但不知何故,我无法让它工作。
This is how far I've come, it finds only the last &
这是我走了多远,它只找到了最后一个&
and also matches the whole string before it and fails to find the other. 并且还匹配它之前的整个字符串并且找不到另一个字符串。 What am I doing wrong? 我究竟做错了什么?
(?>=href\\=\\").*?(&)(?=\\")
Edit: It is not possible to use htmlentities_decode or htmlspecialchars_decode, as there is other Code that would get affected. 编辑:无法使用htmlentities_decode或htmlspecialchars_decode,因为其他代码会受到影响。
The natural way I see without knowing in depth the PHP regex API is to match the string against the pattern until there are no more matches, eg when the last &
我在不深入了解PHP正则表达式API的情况下看到的自然方式是将字符串与模式匹配,直到没有更多匹配,例如当最后一个&
is replaced, there will be no more matches 被替换,将不再有匹配
$str = "<p>Foo & Bar</p> // <-- this should still be &
<a href=\"http://mycoolpage.com/?page=1&fun=true&foo=bar&yes=no\">";
$pattern = "/(href=\".*?)(&)(.*?\">)/";
while (preg_match_all($pattern, $str, $matches)) {
$left = $matches[1][0]; // e.g. href="http://....?page=1
$before = substr($str, 0, strpos($str, $left)); // <p>Foo & ....
$index = strlen($before) + strlen($left);
$str = substr_replace($str, "&", $index, strlen("&"));
}
var_dump($str);
result: 结果:
<p>Foo & Bar</p> // <-- this should still be & <a href="http://mycoolpage.com/?page=1&fun=true&foo=bar&yes=no">
This comment by Wiktor Stribiżew has worked: WiktorStribiżew的评论有效:
Or a harder way: http://ideone.com/ADku3b 或者更难的方式: http : //ideone.com/ADku3b
<?php
$s = '<a href="http://myurl.com/?page=1&fun=true&foo=bar&yes=no">';
echo preg_replace_callback('~(<a\b[^>]*href=)(([\'"]).*?\3|\S+)([^>]*>)~', function ($m) {
return $m[1] . html_entity_decode($m[2]) . $m[4];
}, $s);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.