Jump to content

Help removing ]]> from an api reqest i made


SennNathan

Recommended Posts


<!DOCTYPE html>
<?php
$request = 'http://api.wolframalpha.com/v2/query?appid=YKAERX-QJJAE7L2G6&input=albert%20einstein&format=html';

$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, $request);
$response = curl_exec($curl);
curl_close($curl);
$blank = '';
$remove = ']]>';
$display = preg_replace($remove, $blank, $response);


?>
<html>
<head>
<style>
div.ex
{
width:500px;
padding:10px;
border:5px solid gray;
margin:0px;
}
</style>
</head>

<body>
<div class="ex">
<?php
echo $display
?>
</div>

</body>
</html>

 

Link to comment
Share on other sites

Why even remove it to begin with? It's supposed to be there. You see it in your output, and that's presumably why you're removing it, because you're outputting XML in an HTML document. You're supposed to take the XML they return and display it the way you want, and removing the ]]>s will only screw that up.

Link to comment
Share on other sites

The CDATA is for xml or sgml parsing, what's contained within is often optional but shouldn't be treat as markup (html tags ignored) and should be put out as is, so if fully parsing maybe use htmlentities() on it??? Anyway if you were to strip it out you should either remove the whole thing or also remove the start tag too...

<![CDATA[ ... ]]>
so, after a bit of fiddling (The "Usi" did it even though I haven't looked them up) (also this has only been tested on the given example, but should do all I think/hope)

$s='Stuff before...<![CDATA[<div id="pod_0100" class="pod "><hr class="top" /><h2>
      Input interpretation<span class="colon">:</span></h2><div id="subpod_0100_1" class="sub ">blah blah blah...
	</div></div>
<hr class="bot" /></div>]]>,,, stuff after';

$pattern = '/<!\[CDATA\[(.*)\]\]>/Usi';
$replacement = '---';
echo preg_replace($pattern, $replacement, $s);

EDIT: Sorted ;)

EDIT 2: change $replacement to be an empty string!

Edited by mentalist
Link to comment
Share on other sites

Whao... went back to put right but the forum can't parse it properly, just made it worse, but the missing code just showed the start and end tags for CDATA...

 

 

Actually most of the good stuff has been stripped out too... I'll try to sort it but if can't wait msg me your email Senn and i'll email it you...

Edited by mentalist
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.