0

I have user input which in this case will be Hello

$command = mysqli_real_escape_string($con,$_POST['command']);

a variable called $dialogue_unlock is populated through some scripts. Which for testing will output 'Hello','Goodbye'

$dialogue_unlock .= ',\''. $discuss_row['discussion_text'].'\'';

Im then trying to see if the user input matches the array

$dialogue_array = array($dialogue_unlock);
if(!in_array("$command", $dialogue_array)){
    $err .= 'Invalid Question';
}

As a test im using

echo $err. '<br>';
echo $command. '<br>';
echo $dialogue_unlock;

I get

Invalid Question
Hello
'Hello','Goodbye'

I just cant wrap my head around it as 'hello' is definitely in 'Hello','Goodbye'. Can anybody offer some advise please.

Many thanks.

1
  • 2
    If I understood what you need to do, you have to explode the dialogue_unlock string to get an array of each comma separated value $dialogue_array = explode(",", $dialogue_unlock); Commented Sep 22, 2018 at 20:34

2 Answers 2

1

It looks as though your array contains the 1 element with the text 'Hello','Goodbye' and when you look for Hello it doesn't match. What you should do is add each item of $discuss_row['discussion_text'] to $dialogue_array directly using (in whatever loop you build up $dialogue_unlock)

$dialogue_array[] = $discuss_row['discussion_text'];

So when you add Hello and Goodbye then it will be two separate elements in the array.

Just make sure that before this loop you initialise the array...

$dialogue_array = [];
Sign up to request clarification or add additional context in comments.

1 Comment

You've no idea how much annoyance you saved me. Cant believe i was silly enough to go about this the complete wrong way. That works exactly as i need it. Thank you.
1

Try this.

$command = mysqli_real_escape_string($con,$_POST['command']); /// hello
$dialogue_unlock .= ',\''. $discuss_row['discussion_text'].'\''; /// 'Hello','Goodbye'
$dialogue_unlock = explode(',', $dialogue_unlock); /// dialogue_unlock to array


foreach ($dialogue_unlock as $a) {
    if (strpos($command, $a) !== FALSE) { 
        echo "Match found"; 
    }
   else
    {
        echo "Not found!";
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.