So I'm using an Ajax request and processing input of sentence(s) in my program that will be looked up using a thesaurus API. The processing works fine, but I am trying to get it so that it will strip bad input such as semicolons in order for the API to work, but I'm also trying to get it to display the original input when it's done. So this is basically two strings: one for lookup, one for display. Here is my code:
<?php
$sentence = sanitize($_POST["sentence"]);
$words = explode(" ", $sentence);
foreach ($words as $word)
{
$sanitizedWord = ereg_replace("[^A-Za-z0-9\']", "", $word);
$sanitizedWord = str_replace("\\'", "'", $sanitizedWord);
$sanitizedWord = preg_replace("/\s+/", " ", $sanitizedWord);
// this line of code is causing it not to work!
$sanitizedWord = trim($sanitizedWord);
$test = @json_decode(file_get_contents("http://words.bighugelabs.com/api/2/api_key/$sanitizedWord/json"), true);
if ($test != FALSE)
{
// the rest of my code
}
}
function sanitize($sentence)
{
$sentence = strip_tags($sentence);
$sentence = htmlentities($sentence);
return $sentence;
}
?>
For some strange reason, it doesn't like me trying to use trim on the lookup string. I have to strip whitespace in order to format a valid url, but it never gives me output when I do. What might I be doing wrong?