0

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?

3
Contributors
3
Replies
4
Views
4 Years
Discussion Span
Last Post by HelloJarvis
2

@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);

Edited by LastMitch: grammer

0

You want to remove a semicolon

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.