开发者

Replacing empty paragraph using preg_replace, whitespace not recognized

开发者 https://www.devze.com 2023-02-17 08:01 出处:网络
I need to change this: <p> </p> Into this:开发者_开发知识库 <p class=\"notmobile\"> </p>

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. *


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);


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

Try:

$filecontent  = preg_replace('/<p>\xA0<\/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);


$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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号