Using the string functions

Functions that search a string

strpos($str1, $str2[, $offset])
stripos($str1, $str2[, $offset])
strrpos($str1, $str2[, $offset])
strripos($str1, $str2[, $offset])
str_contains($str1, $str2)
str_starts_with($str1, $str2)
str_ends_with($str1, $str2)

Code that searches a string for spaces

<?php
  $name = 'Martin Van Buren';
  $i = strpos($name, ' ');       // $i is 6
  $i = strpos($name, ' ', $i+1); // $i is 10
  $i = strrpos($name, ' ');      // $i is 10
?>

Code that searches a string a substring

<?php
  $name = 'Martin Van Buren';
  $i = strpos($name, 'Van');     // $i is 7
  $i = strpos($name, 'van');     // $i is FALSE
  $i = stripos($name, 'van');    // $i is 7
  $i = strripos($name, 'A');     // $i is 8
?>

Back