1

I have to generate a php file from the content of a div, this is working. But characters like < and > are blocking the generation.

For example :

<div id="test">
<p> I want to have this in my php file &lt;?php echo "tatata" ?&gt; </p>
</div>

I'm using the function $("#test").html() to get the content before to send it to a php file using ajax.

If I do an alert of this, I get the correct data but when I open the php file generated, I only have :

<div id="test">
<p> I want to have this in my php file

I've tried to use &#60 instead of &lt; I dont know if the problem comes from jQuery or the php code I call with ajax.

But I'm sure the problem is due to the < and > characters, because when I remove them, it works correctly.

Thanks for your help !

Spydaxx
  • 23
  • 8
  • Possible duplicate of [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – treyBake Aug 01 '19 at 11:22
  • @treyBake no. Here I want to get ALL the content (including HTML) of the test div. It's not supposed to print anything. THe content (php and HTML) will be saved in a file to be processed later, not here :) I want to get the content as a string to save it :) – Spydaxx Aug 01 '19 at 11:25
  • 1
    Any `<` character will be interperted as HTML by the browser. If you take the HTML of that file, store it in a `.php` file, then serve it as a `.php` file, it would execute ` – Qirel Aug 01 '19 at 11:28
  • Please add the relevant code. – user3783243 Aug 01 '19 at 11:29
  • @Spydaxx so you want it to output as plain-text? so the PHP never gets executed? – treyBake Aug 01 '19 at 11:30
  • @treyBakei yes that's what I want. I want the content of the div as pure text. But "<" is supposed to be saved as "<" of course. – Spydaxx Aug 01 '19 at 11:34
  • 1
    @Spydaxx then str_replace? did you google this? – treyBake Aug 01 '19 at 11:37
  • or the manual https://www.php.net/manual/en/function.html-entity-decode.php – treyBake Aug 01 '19 at 11:38
  • @treyBake no. I don't know this function. I'm going to the docs. – Spydaxx Aug 01 '19 at 11:39
  • may need to escape the entities: https://stackoverflow.com/questions/24816/escaping-html-strings-with-jquery/13510502 – Pete Aug 01 '19 at 11:41
  • @treyBake i'll try to use str_replace in my php to see if it resolves the problem – Spydaxx Aug 01 '19 at 11:42
  • 1
    @treyBake It works perfectly using str_replace ! Thank you very much ^^ – Spydaxx Aug 01 '19 at 11:56

2 Answers2

1

The suggestion of @treyBake is working perfectly.

As he suggested in the comments, the function str_replace solved the problem. I use it just before to create my php file and it's working.

Thank you @treyBake ! :)

Spydaxx
  • 23
  • 8
0

Have you tried using htmlentities?

e.g.

echo htmlentities('&lt;sometext&gt;');
treyBake
  • 6,440
  • 6
  • 26
  • 57
Dan Gribble
  • 106
  • 1
  • 9