If you're not looking to duplicate the rest of the string, but instead just want the offset, in the spirit of the str*pos() functions, use strcspn()
(PHP 5, PHP 7, PHP 8)
strpbrk — Buscar una cadena por cualquiera de los elementos de un conjunto de caracteres
strpbrk() busca la cadena haystack
por una char_list
.
haystack
La cadena en donde char_list
es buscada.
char_list
Este parámetro es sensible a mayúsculas y minúsculas.
Devuelve una cadena que empieza desde el caracter encontrado, o false
si no se encuentra.
Ejemplo #1 Ejemplo de strpbrk()
<?php
$texto = 'Este es un texto Simple.';
// esto imprime "e es un texto Simple." ya que 'e' coincide primero
echo strpbrk($texto, 'me');
// esto imprime "Simple." ya que los caracteres son sensibles a mayúsculas/minúsculas
echo strpbrk($texto, 'S');
?>
If you're not looking to duplicate the rest of the string, but instead just want the offset, in the spirit of the str*pos() functions, use strcspn()
A little modification to Evan's code to use an array for the second parameter :<?phpfunction strpbrkpos($s, $accept) { $r = FALSE; $t = 0; $i = 0; $accept_l = count($accept); for ( ; $i < $accept_l ; $i++ ) if ( ($t = strpos($s, $accept[$i])) !== FALSE ) if ( ($r === FALSE) || ($t < $r) ) $r = $t; return $r;}?>