0

I am trying to save a XML code as a string in PHP in order to save it into a database later. However, the XML seems to always be executed no matter if I use "" or ' '.

$Cxml = '<Conds><C><FieldNo>119</FieldNo><Filter>' . $variable . '</Filter></C></Conds>';

What am I doing wrong?

2
  • 2
    What behaviour were you expecting and what behaviour are you getting? Commented Apr 4, 2016 at 10:32
  • I am expecting $variable = 2; $Cxml = '<Conds><C><FieldNo>119</FieldNo><Filter>2</Filter></C></Conds>' and I am getting $Cxml = 1192 Commented Apr 4, 2016 at 10:38

1 Answer 1

1

Just replace < by &lt; and > by &gt;

$variable = 12345;
$Cxml = '<Conds><C><FieldNo>119</FieldNo><Filter>' . $variable . '</Filter></C></Conds>';
$temp_var1 = str_replace('<','&lt;',$Cxml);
$temp_var2 = str_replace('>','&gt;',$temp_var1);
echo $temp_var2;

Output : <Conds><C><FieldNo>119</FieldNo><Filter>12345</Filter></C></Conds>

Update :

The htmlspecialchars() function generates the same output. For example:

$Cxml = '<Conds><C><FieldNo>119</FieldNo><Filter>' . $variable . '</Filter></C></Conds>';
echo htmlspecialchars($Cxml);

Explanation :

The HTML character encoder converts all applicable characters to their corresponding HTML entities. Certain characters have special significance in HTML and should be converted to their correct HTML entities to preserve their meanings.

For example, it is not possible to use the < character as it is used in the HTML syntax to create and close tags. It must be converted to its corresponding &lt; HTML entity to be displayed in the content of an HTML page. HTML entity names are case sensitive.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. The output works, however, when I try to save it into the database, < are replaced by &lt and > by &gt.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.