使用 preg_replace 替换空段落,无法识别空格

发布于 2024-10-24 03:05:56 字数 1343 浏览 1 评论 0 原文

我需要将其更改

<p> </p>

为:

<p class="notmobile"> </p>

在字符串上。看起来很简单,但以下方法不起作用:

$filecontent  = preg_replace('/<p> <\/p>/', '<p class="notmobile"> </p>',   $filecontent);
$filecontent  = preg_replace('/^<p> <\/p>/', '<p class="notmobile"> </p>',  $filecontent);
$filecontent  = preg_replace('/<p>\s<\/p>/', '<p class="notmobile"> </p>',  $filecontent);
$filecontent  = preg_replace('/<p>\s+<\/p>/', '<p class="notmobile"> </p>', $filecontent);
$filecontent  = str_replace('<p> </p>', '<p class="notmobile"> </p>',   $filecontent);

为了确保我不会发疯,我对 xxx 进行了替换,将其转换为 yyy,效果很好。我认为问题是我的空间不是普通空间,因为内容可能是 Windows 字符集 iso-8859-1 或其他任何内容(或者它很混乱,因为我们已将其转换为utf-8 沿线某处..)

从 chome/firefox 复制并粘贴空段落也不起作用。

我有点卡住了:(感谢您的帮助!

更新:这是base64_output,AwMD是一串0,我用它来标记上面的p字符串的开头。 wPg0KPHA

+ wqA8L3A+DQo8cD7CoDwvcD4NCjxwPsKgPC9wPg0KPHA+wqA8L3A+DQo8cD7CoDwvcD4NCjxwPsKgPC9wPg0KPHA+wqA8L3A+DQo8cD7CoDwvcD4NCjxwPsKgPC9wPg0KPHA+wqA8L 3A+DQo8cD7CoDwvcD4NCjxwPsKgPC9wPg0KPHA+YmFzZTY0ZW5jb2Rpbmc8L3A+PC9w

*update2:我发现 php 中的字符 ord 值为:194 后跟 160 - 例如,它是两个字符*。

I need to change this:

<p> </p>

Into this:

<p class="notmobile"> </p>

on a string. Seems simple, but the following don't work:

$filecontent  = preg_replace('/<p> <\/p>/', '<p class="notmobile"> </p>',   $filecontent);
$filecontent  = preg_replace('/^<p> <\/p>/', '<p class="notmobile"> </p>',  $filecontent);
$filecontent  = preg_replace('/<p>\s<\/p>/', '<p class="notmobile"> </p>',  $filecontent);
$filecontent  = preg_replace('/<p>\s+<\/p>/', '<p class="notmobile"> </p>', $filecontent);
$filecontent  = str_replace('<p> </p>', '<p class="notmobile"> </p>',   $filecontent);

To make sure I wasn't going crazy, I did a replace on xxx to turn it into yyy which worked just fine. I think the problem is my space isn't a normal space as the content is probably that windows character set iso-8859-1 or whatever it is (or it's got confused because we've converted that to utf-8 somewhere along the line..)

Copying and pasting the empty paragraph from chome/firefox didn't work either.

I'm a bit stuck :( Thanks for helping!

Update: Here's the base64_output, AwMD is a string of 0s which I used to mark the beginning of a string of p's as above.

AwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDA8L3A+DQo8cD7CoDwvcD4NCjxwPsKgPC9wPg0KPHA+wqA8L3A+DQo8cD7CoDwvcD4NCjxwPsKgPC9wPg0KPHA+wqA8L3A+DQo8cD7CoDwvcD4NCjxwPsKgPC9wPg0KPHA+wqA8L3A+DQo8cD7CoDwvcD4NCjxwPsKgPC9wPg0KPHA+wqA8L3A+DQo8cD7CoDwvcD4NCjxwPsKgPC9wPg0KPHA+YmFzZTY0ZW5jb2Rpbmc8L3A+PC9w

*update2: I've found the charater ord values in php are: 194 followed by 160 - eg it's two characters. WEIRD. *

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

逆夏时光 2024-10-31 03:05:56

确实是 NBSP \xA0 的 UTF-8 编码 11000010 10100000。如前所述,这是有效的:

= preg_replace('/<p>\p{Z}*<\/p>/u', '<p class="notmobile"> </p>', $f);

It's indeed the UTF-8 encoding 11000010 10100000 of NBSP \xA0. As said earlier, this works:

= preg_replace('/<p>\p{Z}*<\/p>/u', '<p class="notmobile"> </p>', $f);
心安伴我暖 2024-10-31 03:05:56

它可能是不间断空格   ASCII 代码 0xA0, 160

尝试:

$filecontent  = preg_replace('/<p>\xA0<\/p>/', '<p class="notmobile"> </p>',   $filecontent);

It might be a non-breaking space   ASCII code 0xA0, 160.

Try:

$filecontent  = preg_replace('/<p>\xA0<\/p>/', '<p class="notmobile"> </p>',   $filecontent);
音盲 2024-10-31 03:05:56

为什么不直接将

替换为

呢?

$filecontent = str_replace("<p>", "<p class=\"notmobile\">", $filecontent);

或者您是否尝试将所有

标记(无论内容如何)替换为


对于中间只有 一个 空格的标签对,请尝试像这样替换它:

$filecontent = str_replace("<p> </p>", "<p class=\"notmobile\"> </p>", $filecontent);

Why not just replace <p> with <p class="notmobile">?

$filecontent = str_replace("<p>", "<p class=\"notmobile\">", $filecontent);

Or are you trying to replace all pairs of <p> tags, regardless of content, with <p class="notmobile"> </p>?


For tag pairs with only one space in between, try replacing it like so:

$filecontent = str_replace("<p> </p>", "<p class=\"notmobile\"> </p>", $filecontent);
ι不睡觉的鱼゛ 2024-10-31 03:05:56
$filecontent  = preg_replace('/<p>\xC2\xA0<\/p>/', '<p class="notmobile"> </p>',    $filecontent);

当你意识到一切都不像看上去的那样时,就很容易了!现在修改有用的答案。

$filecontent  = preg_replace('/<p>\xC2\xA0<\/p>/', '<p class="notmobile"> </p>',    $filecontent);

Easy when you realise nothing is as it seems! Modding up useful answers now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文