Jump to content

strpos()


lostprophetpunk

Recommended Posts

I was wondering if you can use an array in the strpos() function, such as...

 

$array = array(1,2,3,4,5,6);

$pos = strpos($array, $text);

 

I am trying to find any of the characters in the string, inside the array. If there is a way...how would I go about it?

 

Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/172814-strpos/
Share on other sites

As stated in the manual the second argument accepts a mix of types.

That didn't really help at all...as it was the wrong function. I have looked up the function I am using but I cannot figure it out.

 

I want to try and get the string position of any of the characters in the array. I am going about it the wrong way?

Link to comment
https://forums.phpfreaks.com/topic/172814-strpos/#findComment-910858
Share on other sites

Sorry, I completely misread your post. Wouldn't really make any sense with strstr either.

 

Assuming you have an array of chars (not numbers) you could easily create a loop to do what you want.

 

$a = array('a','g','h');
$s = 'the dog and cat';
foreach ($a as $v) {
  $pos = strpos($s, $v);
  if ($pos === false) {
    echo "$v not found in $s\n";
  } else {
    echo "$v found at position $pos\n";
  }
}

Link to comment
https://forums.phpfreaks.com/topic/172814-strpos/#findComment-910868
Share on other sites

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.