SennNathan Posted October 27, 2013 Share Posted October 27, 2013 <!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> Quote Link to comment https://forums.phpfreaks.com/topic/283329-help-removing-from-an-api-reqest-i-made/ Share on other sites More sharing options...
trq Posted October 27, 2013 Share Posted October 27, 2013 Do you have a question? Quote Link to comment https://forums.phpfreaks.com/topic/283329-help-removing-from-an-api-reqest-i-made/#findComment-1455648 Share on other sites More sharing options...
SennNathan Posted October 27, 2013 Author Share Posted October 27, 2013 I figured it out just had to change $remove = '/]]>/'; Quote Link to comment https://forums.phpfreaks.com/topic/283329-help-removing-from-an-api-reqest-i-made/#findComment-1455649 Share on other sites More sharing options...
trq Posted October 27, 2013 Share Posted October 27, 2013 Why are you using a regex to remove a fixed string? Just use str_replace. Quote Link to comment https://forums.phpfreaks.com/topic/283329-help-removing-from-an-api-reqest-i-made/#findComment-1455650 Share on other sites More sharing options...
requinix Posted October 27, 2013 Share Posted October 27, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/283329-help-removing-from-an-api-reqest-i-made/#findComment-1455655 Share on other sites More sharing options...
mentalist Posted October 27, 2013 Share Posted October 27, 2013 (edited) 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 October 27, 2013 by mentalist Quote Link to comment https://forums.phpfreaks.com/topic/283329-help-removing-from-an-api-reqest-i-made/#findComment-1455704 Share on other sites More sharing options...
mentalist Posted October 27, 2013 Share Posted October 27, 2013 (edited) 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 October 27, 2013 by mentalist Quote Link to comment https://forums.phpfreaks.com/topic/283329-help-removing-from-an-api-reqest-i-made/#findComment-1455705 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.