PHP Exclude string if it has not already been deleted with objects

I am using a third-party API that seems to be returning its data with existing object codes. Eg The Lion’s Pride.

If I print the as-is string from the API, it will display fine in the browser (in the above example, it will be placed in the apostrophe). However, I cannot trust that the API will always use entities in the future, so I want to use something like htmlentitiesor htmlspecialcharsmyself before I print it. The problem is that it will again encode the ampersand in the object code, and the final result will be The Lion’s Pridein the HTML source, which does nothing convenient for the user.

How to use htmlentitiesor htmlspecialcharsonly if it has not been used in a string yet? Is there a built-in way to detect if entities are already on the line?

+4
source share
6 answers

No one seems to answer your real question, so I will

How to use htmlentities or htmlspecialchars only if it has not been used in the string yet? Is there a built-in way to detect if entities are already on the line?

It's impossible. What if I create a tutorial about HTML objects and I want to actually print this on the screen:

The Lion’s Pride

... it should be encoded as ...

The Lion’s Pride 

, , ?... ..


: , , - - .

, ? , , html XML? JSON?... JSON ? .

, API, , , . API, , , . / .

, , , .

+3

, html-/ , html .

, , , .

0

, . ( html_entity_decode())

$string = htmlspecialchars(html_entity_decode($string));

https://eval.in/662095

0

, !

, .

- .


:

, & , ?

, IS , &, &

0
source

You also have the option of using htmlspecialchars_decode ();

$string = htmlspecialchars_decode($string);
-1
source

This is already in htmlentities:

php > echo htmlentities('Hi&mom', ENT_HTML5, ini_get('default_charset'), false);
Hi&mom
php > echo htmlentities('Hi&mom', ENT_HTML5, ini_get('default_charset'), true);
Hi&mom

Just use the [optional] 4th argument for NOT double encoding.

-1
source

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


All Articles