【发布时间】:2021-08-16 09:38:31
【问题描述】:
假设我有这个字符串:
$string = '<p > ¡Esto es una prueba! < /p > <p> <strong > Prueba 123 </strong> </p> <p> <strong> < a href="https://matricom.net"> MATRICOM < / a> </ strong> </p> <p> <strong > Todas las pruebas aquí ... </strong > < /p>'
我想做的是使用 PHP 修复 HTML 标签(由于空格,它们格式错误)。我尝试了几种不同的正则表达式,我在网上找到了这样的表达式:
$html = trim(preg_replace('/<\s+>/', '<>', $text));
和:
$html = preg_replace('/<(.+?)(?:»| |″)(.+?)>/', '<\1\2>', $text);
我正在尝试获取这样的字符串输出(在 HTML 标记的前面部分和结尾部分删除了空格):
'<p> ¡Esto es una prueba! </p> <p> <strong> Prueba 123 </strong> </p> <p> <strong> <a href="https://matricom.net"> MATRICOM </a> </strong> </p> <p> <strong> Todas las pruebas aquí ... </strong> </p>'
背景故事:Google 翻译倾向于在翻译结果中添加影响 HTML 结构的随机空格。只是寻找一种快速清理标签的方法。我已经搜索了两天如何做到这一点,似乎找不到任何适合我正在寻找的东西。
【问题讨论】:
-
您还尝试了什么?使用简单的字符串替换(从
<到<)或使用 DomDocument 是否有帮助? -
您好,下面的答案有帮助吗?