Reputation: 127
I'm testing a concept to see if it will be suitable for something more complication later.
I have a string and an array. I'd like to search the string to see if it contains any of the values in the array. When it does I'd like it to echo the key and the value of the match from the array. The code I have is this.
<?php
$string = '[red,yellow,[blue,[green';
$colour = array ('red','blue','yellow','green');
foreach($colour as $key => $value){
if(strpos($string,'['.$value)){
echo $key." ".$value."<br>";
}
}
?>
This, I thought, should return anything which matches "[colour"
I thought this would return the result:
0 red
1 blue
3 green
It returns
1 blue
3 green
Does anyone know why it doesn't return a match on [red? Something to to with the [ being right at the start of the string?
Upvotes: 1
Views: 62
Reputation: 1464
strpos
returns string position, red is on position 0
You should change your condition to:
if(strpos($string,'['.$value) !== false){
as false is returned if there is no match at all
Upvotes: 2