PHP – Check if a string contains another string

To check if a string contains another string in PHP, we could use the strpos() function which return the position of the first $needle found in $haystack.

  • strpos($haystack, $needle)


 

The following example demonstrates the proper use of strpos().

<?php
  $content = 'Eureka! - https://ykyuen.wordpress.com';

  if (strpos($content, 'ykyuen') === false) {
    // String not found
    print "Missed.";
  } else {
    // String is found
    print "Bingo!";
  }
?>

 

Please refer to the link below for more information on checking if a string contains another string.

Done =)

Reference:

2 thoughts on “PHP – Check if a string contains another string”

Leave a reply to Andre Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.