Jump to content

[SOLVED] in_array and strpos


Bricktop

Recommended Posts

Hi all,

 

I have an array of text and would like to check if the text in the array is present in a server response.

 

So, this is how I would like it to work:

 

$errors = array(
"Once upon a time",
"A long time ago",
"In a galaxy far far away",
"This script is broke"
);      
if (in_array(strpos($response, $errors)))

 

But the above does not work, because you cannot use strpos on an array.  If I do:

 

$errors = array(
"Once upon a time",
"A long time ago",
"In a galaxy far far away",
"This script is broke"
);      
if (strpos($response, 'Once upon a time') || strpos($response, 'A long time ago) || strpos($response, 'In a galaxy far far away') || strpos($response, 'This script is broke))

 

Then it works fine, but obviously this is not an efficient way of achieveing the result.

 

Anyone have any better ideas?

 

Thanks

Link to comment
Share on other sites

use in_array

 

//edit, wrong one

 

<?php
$errors = array(
"Once upon a time",
"A long time ago",
"In a galaxy far far away",
"This script is broke"
);     
$response = "Once upon a time";
if (in_array($response, $errors)) {
echo "found";
} else {
echo "not found";
}

//script echos found
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.