Find position first occurence of an array of strings in a string

PHP has a function strpos() for finding the position of the first instance of a given value in a string. Is there a way to do this with a needle that is an array of strings? It would give the first occurence:

$str = '1st and 3rd';

str_array_pos($str, array('st', 'nd', 'rd', 'th')) //would return 1 because of 'st'

Answers


You could write one yourself:

function str_array_pos($string, $array) {
  for ($i = 0, $n = count($array); $i < $n; $i++)
    if (($pos = strpos($string, $array[$i])) !== false)
      return $pos;
  return false;
}

By the way, the return value in your example should be 0 and not 1 since array indices start at 0.


Need Your Help

Deleted row in tableview is reappearing when navigating back

ios uitableview

Have friendsviewcontroller in which have uibarbuttonItem to edit friends list and other uibarbuttonitem to create groups for group chatrooms.

Recursive type constraints in CodeDom

c# generics codedom

We are writing a proxy generator using CodeDom and it appears that generic types in a recursive constraint are not being generated.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.