在 PHP 中删除所有 <p>,只有一个 space
Remove all <p> with only one space inside in PHP
我的代码有问题,例如:
<p>Some text here</p>
<p>More text here</p>
<p> </p>
<p> </p>
<p>Some text</p>
我想删除所有 <p> </p>
。
它显示为 <p> </p>
,里面有一个 space,但是当我复制 HTML 时,它在里面显示
。
我试过 str_replace('<p> </p> ', '', $data);
没用。
我也试过 preg_replace('#<p>.*?</>#s', '', $data);
并且它删除了所有 <p>
(因为它应该这样做)但是我无法实现只删除 <p>
和 space(或
?)里面。
将 .*?
替换为 /\s+
也不起作用。
我的确切 PHP 代码是:
$recuperer_trajet_infotrafic_1 = explode('<article class="trajet">', $donnees_infotrafic);
$recuperer_trajet_infotrafic_2 = explode('</article>' , $recuperer_trajet_infotrafic_1[1] );
$recuperer_trajet_infotrafic_3 = preg_replace('#<p>[0-9 /]+/[0-9 /]+</p>#', '', $recuperer_trajet_infotrafic_2[0]);
$trajet_infotrafic = str_replace("/\s?<p>(\s| )*<\/p>/","",$recuperer_trajet_infotrafic_3);
echo '<!-- without str_replace1 -->';
echo $recuperer_trajet_infotrafic_3;
echo '<!-- without str_replace2 -->';
echo '<!-- with str_replace1 -->';
echo $trajet_infotrafic;
echo '<!-- with str_replace2 -->';
我在“查看页面源代码”中得到的内容:
<!-- without str_replace1 -->
<!-- TRAJET -->
<h3>Votre trajet</h2>
<div class="septraf"> </div>
<p><span style="text-decoration: underline;">Dans les 2 sens</span> :<br />- arrêt Mimosas reporté à l'arrêt provisoire placé route de Vannes au niveau de la station essence<br />- arrêt Cravate reporté à l'arrêt Ferrière</p>
<p> </p>
<!-- without str_replace2 -->
<!-- with str_replace1 -->
<!-- TRAJET -->
<h3>Votre trajet</h2>
<div class="septraf"> </div>
<p><span style="text-decoration: underline;">Dans les 2 sens</span> :<br />- arrêt Mimosas reporté à l'arrêt provisoire placé route de Vannes au niveau de la station essence<br />- arrêt Cravate reporté à l'arrêt Ferrière</p>
<p> </p>
<!-- with str_replace2 -->
编辑:两个 <p>
标签之间的字符似乎既不是 space 也不是
因为我无法仅使用 [=28= 来替换它] 或 str_replace(' ','TEST',$data);
我不确定我的理解是否正确。但是,您可以这样做:
$data = [
"<p>Some text here</p>",
"<p>More text here</p>",
"<p> </p>",
"<p> </p>",
"<p>Some text</p>"
];
foreach($data as $key => $para) {
if($para == "<p> </p>") {
$data[$key] = ""; // new value here or just unset($data[$key]);
}
}
好吧,试试这个,让我知道
$new_str = str_replace("<p> </p>", '', $str);
您需要使用当前的实际文本,而不是在 html 浏览器上显示的文本,实际文本是   ;而不是 space。试试这个 php 代码,
$str = "<p>Some text here</p>\n<p>More text here</p>\n<p> </p>\n<p> </p>\n<p>Some text</p>";
echo "Before:\n";
echo $str;
$str = str_replace('<p> </p>', '', $str);
echo "\n\nAfter:\n";
echo $str;
对于将在您可以使用的数据中查找 space 或不间断 space html 实体的不同组合的正则表达式替代方案。
示例https://3v4l.org/treZm
preg_replace('/\s?<p>(\s| )*<\/p>/', '', $data);
扩展匹配意义
\s?
optionally begins with a single whitespace character. followed
by <p>(\s| )*</p>
a paragraph element that contains zero
or more whitespace character or non-breaking space html entity.
结果
<!-- TRAJET -->
<h3>Votre trajet</h2>
<div class="septraf"> </div>
<p><span style="text-decoration: underline;">Dans les 2 sens</span> :<br />- arrêt Mimosas reporté à l'arrêt provisoire placé route de Vannes au niveau de la station essence<br />- arrêt Cravate reporté à l'arrêt Ferrière</p>
这将删除以下组合:
<p></p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
\s<p></p>
\s<p> </p>
\s<p> </p>
\s<p> </p>
\s<p> </p>
\s<p> </p>
\s<p> </p>
\s<p> </p>
keep in mind \s
is not a literal space, but applies to all
whitespace characters \n
, \r
, \t
as well.
If you only want to match horizontal spaces, replace \s
with \h
.
if you only want to match literal spaces, replace \s
with 0
or [ ]
我认为 str_replace
的原始问题是由于末尾的错误 space,数据中可能不存在。
str_replace('<p> </p> ', '', $data);
str_replace('<p> </p>', '', $data);
已更新https://3v4l.org/huv0m
我认为此问题与添加的不匹配 space \xc2\xa0
、alt+0160
或
不匹配的 \s
有关.
preg_replace('/\s?<p>(\s|\xc2\xa0| )*<\/p>/', '', $data);
如果您无法真正确定 <p>...</p>
中的字符,您可以使用正则表达式删除所有包含 2 个或更少字符的 <p>
元素。
例如,您可以使用 preg_replace('/<p>.{0,2}<\/p>/', '' , $data);
.
我的代码有问题,例如:
<p>Some text here</p>
<p>More text here</p>
<p> </p>
<p> </p>
<p>Some text</p>
我想删除所有 <p> </p>
。
它显示为 <p> </p>
,里面有一个 space,但是当我复制 HTML 时,它在里面显示
。
我试过 str_replace('<p> </p> ', '', $data);
没用。
我也试过 preg_replace('#<p>.*?</>#s', '', $data);
并且它删除了所有 <p>
(因为它应该这样做)但是我无法实现只删除 <p>
和 space(或
?)里面。
将 .*?
替换为 /\s+
也不起作用。
我的确切 PHP 代码是:
$recuperer_trajet_infotrafic_1 = explode('<article class="trajet">', $donnees_infotrafic);
$recuperer_trajet_infotrafic_2 = explode('</article>' , $recuperer_trajet_infotrafic_1[1] );
$recuperer_trajet_infotrafic_3 = preg_replace('#<p>[0-9 /]+/[0-9 /]+</p>#', '', $recuperer_trajet_infotrafic_2[0]);
$trajet_infotrafic = str_replace("/\s?<p>(\s| )*<\/p>/","",$recuperer_trajet_infotrafic_3);
echo '<!-- without str_replace1 -->';
echo $recuperer_trajet_infotrafic_3;
echo '<!-- without str_replace2 -->';
echo '<!-- with str_replace1 -->';
echo $trajet_infotrafic;
echo '<!-- with str_replace2 -->';
我在“查看页面源代码”中得到的内容:
<!-- without str_replace1 -->
<!-- TRAJET -->
<h3>Votre trajet</h2>
<div class="septraf"> </div>
<p><span style="text-decoration: underline;">Dans les 2 sens</span> :<br />- arrêt Mimosas reporté à l'arrêt provisoire placé route de Vannes au niveau de la station essence<br />- arrêt Cravate reporté à l'arrêt Ferrière</p>
<p> </p>
<!-- without str_replace2 -->
<!-- with str_replace1 -->
<!-- TRAJET -->
<h3>Votre trajet</h2>
<div class="septraf"> </div>
<p><span style="text-decoration: underline;">Dans les 2 sens</span> :<br />- arrêt Mimosas reporté à l'arrêt provisoire placé route de Vannes au niveau de la station essence<br />- arrêt Cravate reporté à l'arrêt Ferrière</p>
<p> </p>
<!-- with str_replace2 -->
编辑:两个 <p>
标签之间的字符似乎既不是 space 也不是
因为我无法仅使用 [=28= 来替换它] 或 str_replace(' ','TEST',$data);
我不确定我的理解是否正确。但是,您可以这样做:
$data = [
"<p>Some text here</p>",
"<p>More text here</p>",
"<p> </p>",
"<p> </p>",
"<p>Some text</p>"
];
foreach($data as $key => $para) {
if($para == "<p> </p>") {
$data[$key] = ""; // new value here or just unset($data[$key]);
}
}
好吧,试试这个,让我知道
$new_str = str_replace("<p> </p>", '', $str);
您需要使用当前的实际文本,而不是在 html 浏览器上显示的文本,实际文本是   ;而不是 space。试试这个 php 代码,
$str = "<p>Some text here</p>\n<p>More text here</p>\n<p> </p>\n<p> </p>\n<p>Some text</p>";
echo "Before:\n";
echo $str;
$str = str_replace('<p> </p>', '', $str);
echo "\n\nAfter:\n";
echo $str;
对于将在您可以使用的数据中查找 space 或不间断 space html 实体的不同组合的正则表达式替代方案。
示例https://3v4l.org/treZm
preg_replace('/\s?<p>(\s| )*<\/p>/', '', $data);
扩展匹配意义
\s?
optionally begins with a single whitespace character. followed by<p>(\s| )*</p>
a paragraph element that contains zero or more whitespace character or non-breaking space html entity.
结果
<!-- TRAJET -->
<h3>Votre trajet</h2>
<div class="septraf"> </div>
<p><span style="text-decoration: underline;">Dans les 2 sens</span> :<br />- arrêt Mimosas reporté à l'arrêt provisoire placé route de Vannes au niveau de la station essence<br />- arrêt Cravate reporté à l'arrêt Ferrière</p>
这将删除以下组合:
<p></p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
\s<p></p>
\s<p> </p>
\s<p> </p>
\s<p> </p>
\s<p> </p>
\s<p> </p>
\s<p> </p>
\s<p> </p>
keep in mind
\s
is not a literal space, but applies to all whitespace characters\n
,\r
,\t
as well.If you only want to match horizontal spaces, replace
\s
with\h
.if you only want to match literal spaces, replace
\s
with0
or[ ]
我认为 str_replace
的原始问题是由于末尾的错误 space,数据中可能不存在。
str_replace('<p> </p> ', '', $data);
str_replace('<p> </p>', '', $data);
已更新https://3v4l.org/huv0m
我认为此问题与添加的不匹配 space \xc2\xa0
、alt+0160
或
不匹配的 \s
有关.
preg_replace('/\s?<p>(\s|\xc2\xa0| )*<\/p>/', '', $data);
如果您无法真正确定 <p>...</p>
中的字符,您可以使用正则表达式删除所有包含 2 个或更少字符的 <p>
元素。
例如,您可以使用 preg_replace('/<p>.{0,2}<\/p>/', '' , $data);
.