Dylan
Dylan

Reputation: 9383

PHP: replace < between certain tags?

In PHP, I have a template with HTML and javascript in it :

<script>
  if (a < b) {
    alert(a);
  } 
</script>
<div>
  hello
</div>

This is fed into a DOMDocument with the loadXML method, but this procudes an error because of the < character inside the script. I know I could possibly use the loadHTML method instead, but for now I just need a quick fix that replaces the < character inside the script tags, and then replace it later again with a <

So the question is, what is the best method to replace the < character, but ONLY inside the script tags?

Upvotes: 0

Views: 121

Answers (3)

Carlos
Carlos

Reputation: 5072

Below there is a way to escape scripts within a given XHTML piece of code.

This code:

<?php

$code = <<<'CODE'
<script>
  if (a < b) {
    alert(a);
  } 
</script>
<div>
  hello
</div>
CODE;

echo preg_replace(
    '@(\<script(?:\s+[^>]*)?\>)([[:alnum:][:punct:][:space:]]+)(\</script\>)@',
    "\\1\n//<![CDATA[\n\\2\n//]]>\n\\3",
    $code);

Produces this:

<script>
//<![CDATA[

  if (a < b) {
    alert(a);
  } 

//]]>
</script>
<div>
  hello
</div>

Upvotes: 0

Yanick Rochon
Yanick Rochon

Reputation: 53606

Use a CDATA for your script if you're using an XML parser.

Upvotes: 1

John Kugelman
John Kugelman

Reputation: 362017

If you can modify the HTML, the best solution is to wrap your JavaScript code with a CDATA section using the <![CDATA[ and ]]> begin and end markers. That will make the document valid XML so it can be parsed by an XML parser.

<script>
// <![CDATA[

  if (a < b) {
    alert(a);
  } 

// ]]>
</script>

Upvotes: 6

Related Questions