Bricktop Posted August 3, 2009 Share Posted August 3, 2009 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 More sharing options...
Daniel0 Posted August 3, 2009 Share Posted August 3, 2009 Use a loop. Link to comment https://forums.phpfreaks.com/topic/168632-solved-in_array-and-strpos/#findComment-889569 Share on other sites More sharing options...
Mardoxx Posted August 3, 2009 Share Posted August 3, 2009 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 https://forums.phpfreaks.com/topic/168632-solved-in_array-and-strpos/#findComment-889573 Share on other sites More sharing options...
FD_F Posted August 3, 2009 Share Posted August 3, 2009 foreach($errors as $check) { strpos($response, $check); } Link to comment https://forums.phpfreaks.com/topic/168632-solved-in_array-and-strpos/#findComment-889574 Share on other sites More sharing options...
Bricktop Posted August 3, 2009 Author Share Posted August 3, 2009 Thanks for the help guys - didn't think of using loops! Link to comment https://forums.phpfreaks.com/topic/168632-solved-in_array-and-strpos/#findComment-889579 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.