tags:

views:

116

answers:

3

Each line is a string

 4 
 minutes 
 12
 minutes
 16
 minutes

I was able to remove the  successfully using str_replace but not the HTML entity. I found this question: http://stackoverflow.com/questions/657643/how-to-remove-html-special-chars-in-php

But the preg_replace did not do the job. How can I remove the HTML entity and that A?

Edit: I think I should have said this earlier: I am using DOMDocument::loadHTML() and DOMXpath. Edit: Since this seems like an encoding issue, I should say that this is actually all separate strings.

A: 

This looks like an encoding error - your document is encoded with UTF-8, but is being rendered as ASCII. Solving your encoding mis-match will solve your issues. You could try using utf8_decode() on your source before using DOMdocument::loadHTML()

Here's an alternative solution from the DOMdocument::loadHTML() documentation page.

Just Jake
Haha, I actually just saw that comment and I am testing it at the moment.
Doug
It did not seem to work for me.
Doug
A: 
$data =  $retrieve_data->item($i)->textContent;
$Stripped = str_replace( html_entity_decode( " " ) , "" , $data );

I ended up just replacing it for now, but it is really just a "ghetto" fix. So if someone can help me out with the encoding, it would be great!

Doug
+3  A: 

Alright - I think I've got a handle on this now - I want to expand on some of the encoding errors that people are getting at:

This seems to be an advanced case of Mojibake, but here is what I think is going on. MikeAinOz's original suspicion that this is UTF-8 data is probably true. If we take the following UTF-8 data:

4 minutes

Now, remove the HTML entity, and replace it with the character it actually corresponds with: U+00A0. (It's a non-breaking space, so I can't exactly "show" you. You get the string: "4 minutes". Encode this as UTF-8, and you get the following byte sequence:

characters:  4  [nbsp]   m   i   n ...
bytes     : 34  C2  A0  6D  69  6E ...

(I'm using [nbps] above to mean a literal non-breaking space (the character, not the HTML entity  , but the character that represents. It's just whitespace, and thus, difficult.) Note that the [nbps]/U+00A0 (non-breaking space) takes 2 bytes to encode in UTF-8.

Now, to go from byte stream back to readable text, we should decode using UTF-8, since that's what we encoded in. Let us use ISO-8859-1 ("latin1") - if you use the wrong one, this is almost always it.

bytes     : 34  C2      A0  6D  69  6E ...
characters:  4   Â  [nbsp]   m   i   n ...

And switch the raw non-breaking space into its HTML entity representation, and you get what you have.

So, either your PHP stuff is interpreting your text in the wrong character set, and you need to tell it otherwise, or you are outputting the result somehow in the wrong character set. More code would be useful here -- where are you getting the data you're passing to this loadHTML, and how are you going about getting the output you're seeing?


Some background: A "character encoding" is just a means of going from a series of characters, to a series of bytes. What bytes represent "é"? UTF-8 says C3 A9, whereas ISO-8859-1 says E9. To get the original text back from a series of bytes, we must know what we encoded it with. If we decode C3 A9 as UTF-8 data, we get "é" back, if we (mistakenly) decode it as ISO-8859-1, we get "é". Junk. In psuedocode:

utf8-decode ( utf8-encode ( text-data ) )           // OK
iso8859_1-decode ( iso8859_1-encode ( text-data ) ) // OK
iso8859_1-decode ( utf8-encode ( text-data ) )      // Fails
utf8-decode ( iso8859_1-encode ( text-data ) )      // Fails

this isn't PHP code, and isn't your fix... it's just the crux of the problem. Somewhere, over the large scale, that's happening, and things are confused.

Thanatos
Awesome explanation. I ran out of upvotes though :(
Jimmie Lin
This is a great answer. Thank you! I hope more people can upvote this.
Doug