Php 替换内部的所有空间<;a>;标签
我已经为此挣扎了一段时间,希望有人能帮我。例如,我需要使用正则表达式替换锚标记内的所有空格Php 替换内部的所有空间<;a>;标签,php,regex,Php,Regex,我已经为此挣扎了一段时间,希望有人能帮我。例如,我需要使用正则表达式替换锚标记内的所有空格 Hello this is a string, check this <a href="http://www.google.com">Google is cool</a> Oh and this <a href="http://www.google.com/blah blah">Google is cool</a> That is all 您好,这是一个字
Hello this is a string, check this <a href="http://www.google.com">Google is cool</a> Oh and this <a href="http://www.google.com/blah blah">Google is cool</a> That is all
您好,这是一个字符串,请检查这个哦,这就是全部
需要成为
Hello this is a string, check this <a[SPACE]href="http://www.google.com">Google[SPACE]is[SPACE]cool</a> Oh and this <a[SPACE]href="http://www.google.com/blah[SPACE]blah">Google[SPACE]is[SPACE]cool</a> That is all
您好,这是一个字符串,请检查这个哦,这就是全部
我们处理的是regex和XMLish字符串-尽管以下内容适用于给定的测试用例,但不同的测试用例的里程可能会有所不同;小心使用
<?
function replace($matches)
{
return preg_replace("/ /", "[SPACE]", $matches[0]);
}
$s = 'Hello this is a string, check this <a href="http://www.google.com">Google is cool</a> Oh and this <a href="http://www.google.com/blah blah">Google is cool</a> That is all';
echo "Before::......\n\n$s\n\nAfter::......\n\n";
echo preg_replace_callback('#<a\b(.+?)</a>#', 'replace', $s);
echo "\n";
?>
预更换(
“/(哦,这就是全部”
);
我不知道该怎么做,因为我像瘟疫一样避免使用正则表达式。与其给你一个真正的答案,我倒不如建议你使用PHP的内置函数str_replace()。-尽管你可能需要使用正则表达式,但我认为没有理由这样做。
Before::......
Hello this is a string, check this <a href="http://www.google.com">Google is cool</a> Oh and this <a href="http://www.google.com/blah blah">Google is cool</a> That is all
After::......
Hello this is a string, check this <a[SPACE]href="http://www.google.com">Google[SPACE]is[SPACE]cool</a> Oh and this <a[SPACE]href="http://www.google.com/blah[SPACE]blah">Google[SPACE]is[SPACE]cool</a> That is all
preg_replace(
'/(<a .+?<\/a>)/e',
'str_replace(" ", "[SPACE]", "\1")',
'Hello this is a string, check this <a href="http://www.google.com">Google is cool</a> Oh and this <a href="http://www.google.com/blah blah">Google is cool</a> That is all'
);