5

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

5
  • Can you post the string encoded in some unambiguous format, such as hex or base64 so that we can see exactly what characters it consists of? Commented Mar 17, 2011 at 22:32
  • How would I do that? The paragraph is inserted by tinymce in our cms, cms saves the content into a mysql database and then the cms displays the page from the database. Commented Mar 17, 2011 at 22:34
  • Your fourth preg_replace is what you want. And more importantly: it's working fine. You might change the + into * and add the /ims flags. Otherwise there is something else in your source text. (Existing attributes in the <p> tags for example?) Commented Mar 17, 2011 at 22:36
  • To test your theory, you can print each character of the empty paragraph using ord(). Or your editor might support printing the hex value of a character, e.g. in Vim, hover over the letter and type ga. Commented Mar 17, 2011 at 22:36
  • Should it be some other whitespace character, then use /\p{Z}+/u to kill it. Commented Mar 17, 2011 at 22:38

4 Answers 4

7

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Mario, I couldn't figure out how to get your regexp in there properly. Answer accepted!
1

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

Try:

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

1 Comment

You were heading in the right direction. Funny that C2A0 is displayed as just one character.. I wonder what C2 actually is..
1

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

3 Comments

Because I only want to replace empty paragrahs generated by tinymce with that, not paragraphs with content.
Then just replace <p> </p> with <p class="notmobile"> </p>.
The point was the space wasn't a regular space, it was hex C2A0 (eg two funny characters glued together)
0
$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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.