Reputation: 804
I am scraping some sites, and have ASCII text that I want to convert to plain text for storing in a DB. For example I want
I have got to tell anyone who will listen that this is
one of THE best adventure movies I've ever seen.
It's almost impossible to convey how pumped I am
now that I've seen it.
converted to
I have got to tell anyone who will listen that this is
one of THE best adventure movies I've ever seen. It's
almost impossible to convey how pumped I am now that
I've seen it.
I have googled my fingers bloody, any help?
Upvotes: 11
Views: 31803
Reputation: 1761
You can use html_entity_decode
:
echo html_entity_decode('...', ENT_QUOTES, 'UTF-8');
Few notes:
Please note that it looks like you actually want to convert from HTML-encoded string(with entities like 
) to ASCII AKA plaintext.
This example converts to UTF-8 which is ASCII-compatible character encoding for all ASCII characters (i.e. with char codes below 128). If you really want plain ASCII (thus loosing all accented characters and characters from foreign languages) you should strip all offending characters separately.
Last argument ('UTF-8') is necessary to keep compatibility with different PHP versions since the default value has changed since PHP 5.4.0.
Update: Example with your text in ideone.
Update2: Changed ENT_COMPAT to ENT_QUOTES by @Daan's suggestion.
Upvotes: 29