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?

Recommended Answers

All 3 Replies

Member Avatar for LastMitch

@HelloJarvis

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:

1) You want to remove a semicolon
2) This line is not working: $sanitizedWord = trim($sanitizedWord);

Here is how you remove a semicolon

preg_replace('/[;]/', '', $word);

Regarding about this line:

$sanitizedWord = trim($sanitizedWord);

It should be just $word not $sanitizedWord

$sanitizedWord = trim($word);

You want to remove a semicolon

The first ereg_replace removes everything that's not a letter or a single quote.

Please, that doesn't help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.