Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert ASCII to plaintext in PHP

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?

like image 778
e_r Avatar asked May 15 '12 07:05

e_r


People also ask

How do you convert ASCII?

You must first convert the character to its ASCII value. In LiveCode, this is done with the charToNum function. Converting a number to the corresponding character is done with the numToChar function. The first of these statements converts a number to a character; the second converts a character to its ASCII value.

Is CHR () and Ord () is opposite function in PHP?

Description. The chr() function generates a character from the specified ASCII value. This function is the inverse of the ord() function which does the opposite of what this function does.


1 Answers

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.

like image 140
ash108 Avatar answered Oct 12 '22 00:10

ash108