删除PHP中字符串中多余的“>”
在我的代码中,我得到一个字符串,其中包含如下html标记:删除PHP中字符串中多余的“>”,php,regex,Php,Regex,在我的代码中,我得到一个字符串,其中包含如下html标记: $string = '<div style="width:100px;">ABC 1234 <span> Test string, testing this string</span></div>'; 我的问题是,为什么我突然获得了一个EXCESS>?我是否可以使用一个不同的正则表达式,在不显示额外>的情况下去掉style属性?还是有什么办法让我能骑上它 我试过两次使用str_repla
$string = '<div style="width:100px;">ABC 1234 <span> Test string, testing this string</span></div>';
我的问题是,为什么我突然获得了一个EXCESS>?我是否可以使用一个不同的正则表达式,在不显示额外>的情况下去掉style属性?还是有什么办法让我能骑上它
我试过两次使用str_replace,如下所示:
$string = str_replace("\n", "", $string);
$string = str_replace(">>", ">", $string);
但这也不起作用
我不是要删除HTML标记,只是删除样式部分。我使用了这个
$string = '<div style="width:100px;">ABC 1234 <span> Test string, testing this string</span></div>';
$output = preg_replace('/(<[^>]+) style=".*?"/i', '$1', $string);
die(htmlentities($output))
输出是
<div>ABC 1234 <span> Test string, testing this string</span></div>
根据需要仅将其用于此字符串
<?php
$string = "<div style=\"width:100px;\">ABC 1234 <span> Test string, testing this string</span></div>";
$string = strip_tags($string,"<span>");
$string = "<div>".$string."</div>";
?>
现在字符串是:
<div>ABC 1234 <span> Test string, testing this string</span></div>
您的字符串错误您应该使用类似于$string='ABC 1234 Test string,testing this string'的内容您的完整替换代码是什么?不仅仅是正则表达式。无法复制结果字符串。我得到了ABC 1234测试字符串,只作为替换的结果来测试这个字符串。我想我得到的是said>,因为我的字符串上有更多的html标记。在任何情况下,在preg_替换之前使用addslashes,而不是相反的方法似乎已经成功了。谢谢你的帮助。这正是我所做的,最后得到了多余的>谢谢,我来试试这个。也许我所需要的只是斜线。
<?php
$string = "<div style=\"width:100px;\">ABC 1234 <span> Test string, testing this string</span></div>";
$string = strip_tags($string,"<span>");
$string = "<div>".$string."</div>";
?>
<div>ABC 1234 <span> Test string, testing this string</span></div>