Mishra Shreyanshu
Mishra Shreyanshu

Reputation: 644

Convert special character '&' , '>' and '<' to their respective character reference for XML

I need to replace only these 3 symbol for my XML:

  1. & to &
  2. < to &lt;
  3. > to &gt;

I used htmlspecialchars but it will replace single and double quotes too.

Upvotes: 2

Views: 71

Answers (1)

fox_db
fox_db

Reputation: 58

Have a look at the htmlspecialchars documentation. You can use flags to ignore quotes (you might be interested in the ENT_NOQUOTES flag).

Here's an example from the same article :

<?php
$str = "Jane & 'Tarzan'";
echo htmlspecialchars($str, ENT_COMPAT); // Will only convert double quotes
echo "<br>";
echo htmlspecialchars($str, ENT_QUOTES); // Converts double and single quotes
echo "<br>";
echo htmlspecialchars($str, ENT_NOQUOTES); // Does not convert any quotes
?>

Upvotes: 2

Related Questions