Replace HTML tags

Asked

Viewed 83 times

0

I have a PHP page that displays POSTS (BLOG style).

Only sometimes I put some commands in the text itself (html tags etc).

How can I do to:

1 - -> If you have something like Alert() it just shows on the page this command, but does not execute it.

Thank you

2 answers

1

To display the HTML elements, just use the function htmlentities:

echo htmlentities("<script>alert('SOpt')</script>");

The exit will be:

&lt;script&gt;alert('SOpt')&lt;/script&gt; 

And so the browser, instead of executing the code, will only display it.

-2

Well, I don’t know if I understand it very well, but since Alert you would have the console.().

Instead of Alert('Hello world'), you would put console.log('Hello world'). In the case of Chrome, press F12 and open the console tab. So everything you send through the.log console will be shown in this tab silently, so the user won’t be able to see. Obs, this is for javascript codes.

In addition to the console.log, you have a console.warn that displays the text in dark yellow and with an exclamation signal, serves to draw attention. I also have the console.error that displays the message you want in the form of errors.

If you wanted to send a PHP variable to the.log console, you could use this code:

<?php
    $array = array("um" => 1,"dois" => 2, 3, 4);
    $debug = json_encode($array);
    echo "<script>console.log($debug)</script>";
?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.