0

I have the following code witch is used to display HTML from an XML file. The XML file itself comes from a form POST where a "\" is added in after various HTML tags.

Is there anyway I can modify the script below to hide/remove the "\" when the XML contents are displayed?

<?php
$file="data/_data1.xml";

$xmlDoc = new DOMDocument();
$xmlDoc->load($file);

//shows specific node data
$x=$xmlDoc->getElementsByTagName( "_tag_1" );
$x=$x->item(0)->nodeValue;
//show result
echo $x;
echo "<br />";
?>

I've tried using preg_replace but it still doesn't remove the "\"

<?php
<?php
$file="data/_data1.xml";

$xmlDoc = new DOMDocument();
$xmlDoc->load($file);

//shows specific node data
$x=$xmlDoc->getElementsByTagName( "_tag_1" );
$x=$x->item(0)->nodeValue;
//remove backslash
$xx=trim(preg_replace('/\+/', ' ', $x));
//show result
echo $xx;
echo "<br />";
?>

Anyone got any other ideas how I can hide/remove the "\"?

Thanks.

3
Contributors
6
Replies
7
Views
6 Years
Discussion Span
Last Post by ko ko
-1

I don't really understand what you mean with added backslashes, but in regular expressions, you need to escape them. So, to remove one or more backslashes, use:

preg_replace('/\\+/', '', $x)
0

Thanks twiss, I have updated it to the following:

<?php
$file="data/_data.xml";  

$xmlDoc = new DOMDocument();
$xmlDoc->load($file);

//shows specific node data
$x=$xmlDoc->getElementsByTagName( "_tag_1" );
$x=$x->item(0)->nodeValue;

//remove backslash and show result
echo preg_replace('/\\+/', '', $x)
echo "<br />";
?>

I can see the backslashes are still there though so it isn't working! Below you can see the XML file I am trying to use. As you can see the backslashes always appear on hyper links, and thus when info is displayed the hyper links don't work!...

<?xml version="1.0" encoding="UTF-8" ?> 
- <data_store>
  <_data_1>
   <b>Telephone:</b> 123456</br><br> <b>Email:</b> <a href=\"http://xyz.com/contact-form.php/\" />xyz@xyz.com</a> <b>Address:</b><br><br><br> <a href=\"http://google.com/\" />Google...</a>
  </_data_1> 
  </data_store>

I have tried this with both ISO-8859-1, and UTF-8 XML encoding to see if that is effecting it, but both yield the same results.

Would it perhaps be better to find out why the "\" is inserted on the POST in the first place? so that it doesn't get saved to the xml file anyway?

I've had a look at the edit XML file script and cant see any reason for the "\" which is what leads me to believe it is added at the POST...

Edited by NuGG: n/a

0

Perhaps magic quotes are on. Try if using stripslashes() works instead of the replace.

0

Awesome. Done some research and a lot of trial and error and came up with this:

<?php
$file="data/_data1.xml";  

$xmlDoc = new DOMDocument();
$xmlDoc->load($file);

//shows specific node data
$x=$xmlDoc->getElementsByTagName( "_tag_1" );
$x=$x->item(0)->nodeValue;

//replace
$x = ereg_replace("[<a href=\'[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]]/\" />]","<a href=\"\\0\">\\0</a>", $x);

//show result
echo $x;
echo "<br />";
?>

Which takes...

<a href=\"http://example.com\">LINK</a>

...and replaces it with...

<a href="http://example.com">LINK</a>

...when displayed.

Works a treat!

Edited by NuGG: extra / !!

0

Okay, well, stripslashes does the same, but not only in a tags of course. But glad you found something :)

0

Note that "ereg_replace" is deprecated. Use "preg_replace" instead of "ereg_replace".

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.