Search code examples
phpxmlcdata

How convert CDATA xml with more single and double quote in a string php?


Sorry for stupid question...

i have in file xml a CDATA in children node:

  <placemark>
   <description><![CDATA[ <h2>ML 2.1 - GREATER LOS ANGELES' AREA, CALIF.</h2><br><b>2016-09-29   16:10:00.3 UTC</b><br /><br /><table style="width:100%;"><tr><td><b>Latitude</b></td><td style="padding-left:5px;">  34.13 N<br></td></tr><tr><td><b>Longitude</b></td><td style="padding-left:5px;">  119.14 W<br></td></tr><tr><td><b>Deph</b></td><td style="padding-left:5px;">  20   Km<br></td></tr><tr><td><b>Magnitude</b></td><td style="padding-left:5px;">  ML 2.1<br></td></tr></table><br /><br /><a href="http://www.emsc-csem.org/Earthquake/earthquake.php?id=534164"> Link to the event </a> ]]></description>
  </placemark>

I get CDATA but i don't know how coonvert it in string php

How can add for' used in CDATA backslash escape \ ? In this way than is easy...just write

  $file= simplexml_load_file(FILE);
  $CDATA = $file -> placemark -> description;
  $string= "'".$CDATA."'"

I hope you can help me and sorry for my english, thanks a lot in advice !


Solution

  • Literals are how you write values in source code. In $foo = "abc"; the "abc" is the string literal.

    The $file->placemark->description is an instance of SimpleXMLElement and can be cast into a string.

    Here is an example for an explicit cast:
    $string = (string)$file->placemark->description;

    But it can be implicit, like using it in a string context:
    echo $file->placemark->description;

    I am not sure why you're trying to add quotes to it. If you try to output the string to JavaScript, you should try json_encode().