Forum Moderators: coopster

Message Too Old, No Replies

php displaying data from Cdata xml tag

         

msthac01

3:11 pm on Feb 6, 2008 (gmt 0)

10+ Year Member



I'm trying to extract the data from a Cdata tag in an xml file so I can decode and display it in pdf format. Here's part of the tag I'm trying to extract from

<![CDATA[JVBERi0xLjMKJeTjz9IKMSAwIG9iago8PC
9UeXBlIC9QYWdlcyAKL0NvdW50IDYgCi9LaWRzIFs0IDAgUiAKMTEgMCBSIAoxNSAwIFIgCjE5IDAgUiAKMjMgMCBSIAoyNyAwIFIgCl0KPj5l.....
RkUzNzNBOTgxQTREMj48M0Q1N0NDMDFFRTg0ODhBOUYzRTMxNEMwNUFDQ0I1OTg+XQo+PgpzdGFydHhyZWYKMTE2MzMKJSVFT0YK ]]>

here's what I've been trying so far

$fp5 = fopen("Receive.xml", "rb");
$theSize = filesize("Receive.xml");
$data = fread($fp5, $theSize);
$TheData = preg_match_all("/<!\[\CDATA\[(.*?)\]\]>/", $data, $string);
print_r($string);

I'm getting nothing at all just empty array. Any suggestions? I'm on php 4.4.4 so some of the newer methods aren't available to me.

phparion

4:46 pm on Feb 6, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I can give you a dirty solution as follow,

<?php

$data = "<![CDATA[JVBERi0xLjMKJeTjz9IKMSAwIG9iago8PC
9UeXBlIC9QYWdlcyAKL0NvdW50IDYgCi9LaWRzIFs0IDAgUiAKMTEgMCBSIAoxNSAwIFIgCjE5IDAgUiAKMjMgMCBSIAoyNyAwIFIgCl0KPj5l.....
RkUzNzNBOTgxQTREMj48M0Q1N0NDMDFFRTg0ODhBOUYzRTMxNEMwNUFDQ0I1OTg+XQo+PgpzdGFydHhyZWYKMTE2MzMKJSVFT0YK ]]>";

//echo $data."<pre>";
$b=explode("CDATA[",$data);
echo "<pre>"; print_r($b);

$rep = array(" ","]",">");
echo str_replace($rep,"",$b[1]);
?>

but at the same time, I strong recommend you to use some ready made PHP class, if you cannot write one, from websites like phpclasses.org to parse your xml in a more organized method. You can also use simpleXML() with PHP5.

msthac01

5:07 pm on Feb 6, 2008 (gmt 0)

10+ Year Member



There is more in the file then just the Cdata tag thats where I'm having the issue, just pulling out that tags data only, I can pull the entire xml file into a variable, but then can't get the cdata tag portion out of the variable

PHP_Chimp

12:51 pm on Feb 7, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$TheData = preg_match_all("/<!\[\CDATA\[(.*?)\]\]>/", $data, $string);

I take it that your xml file is not all on a single line? So there are \n characters in there. If so your expression for the <![CDATA[ will fail as the . doesnt match a new line character.
If you use the s modifier [uk2.php.net] does your expression work then?

msthac01

4:13 pm on Feb 7, 2008 (gmt 0)

10+ Year Member



yes the cdata tag is on multiple lines

I tried

$TheData = preg_match_all("/<!\[CDATA\[(.*?)\]\]>/s", $data, $string);
print_r($string);

I got the data out of the cdata tag but each line in the xml file is on a separate line in the variable, which when trying to use the base64_decode( $string ), it doesn't properly create the pdf file due to the line spacing in the variable, how can I get the lines to run together on one continuous line?

PHP_Chimp

6:12 pm on Feb 7, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



as the . is now matching that newline character this will be returned in the output. So you can use str_replace [uk3.php.net] to replace it.

$input = str_replace('\n', '', $input);

msthac01

7:34 pm on Feb 7, 2008 (gmt 0)

10+ Year Member



I've tried using

str_replace('\n', '', $string);

and other variations using \t \r etc but still the lines are split, can the preg_match_all be changed to not include new line returns

msthac01

9:36 pm on Feb 7, 2008 (gmt 0)

10+ Year Member



I'm to this now and still no luck in removing the carriage returns so that I can create the pdf file

$TheData = preg_match_all("/<!\[CDATA\[(.*?)\]\]>/s", $data, $string);
$testing = $string[1][0];
$testing = str_replace('\n', '', $testing);
echo $testing;

Still getting the multiline text file have tried many variations of '\n', '\r\n', '\r', etc, still no luck

msthac01

9:50 pm on Feb 7, 2008 (gmt 0)

10+ Year Member



Finally got everything to work, had to get the cdata tag, place the array from preg_match_all into a variable, then strip the carriage returns, then run the new var through base64_decode and voilah! a pdf thanks for everyones help and patience with a new php programming learning the hard way. Here's my code

$TheData = preg_match_all("/<!\[CDATA\[(.*?)\]\]>/s", $data, $string);
$testing = $string[1][0];
$testing = str_replace(array("\r", "\n", "\t", " "), "", $testing);
echo $testing;

$filename = "test.pdf";
/* encode & write data (binary) */
$ifp = fopen($filename, "wb" );
fwrite( $ifp, base64_decode( $testing ) );
fclose( $ifp );