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
https://forums.phpfreaks.com/topic/168632-solved-in_array-and-strpos/
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
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.