Convert ASCII to plaintext in PHP

I clean some sites and have ASCII text that I want to convert to plain text for storage in the database. 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 google my fingers are bloody, any help?

+6
source share
1 answer

You can use html_entity_decode :

 echo html_entity_decode('...', ENT_QUOTES, 'UTF-8'); 

A few notes:

  • Please note that in fact you want to convert from an HTML-encoded string (with objects like  ) to plaintext ASCII AKA.

  • This example converts to UTF-8 , which is the ASCII character encoding for all ASCII characters (i.e. with char codes below 128). If you really need simple ASCII (thus losing all accented characters and characters from foreign languages), you should separate all offensive characters separately.

  • The last argument ('UTF-8') is required for compatibility with different versions of PHP, since the default value has changed since PHP 5.4.0.

Update: Example with text in ideone .

Update 2: changed ENT_COMPAT to ENT_QUOTES at the suggestion of @Daan.

+18
source

Source: https://habr.com/ru/post/915754/


All Articles