contains php function code example

Example 1: php check if string contains word

$myString = 'Hello Bob how are you?';
if (strpos($myString, 'Bob') !== false) {
    echo "My string contains Bob";
}

Example 2: php find if string contains

if (strpos($string, 'substring') !== false) {
	// do stuff 
}

Example 3: php string contains

$mystring = 'abc';
$findme   = 'a';
$pos = strpos($mystring, $findme);

Example 4: php needle haystack

//Find the position of the first occurrence of a substring in a string
$mystring = 'abc';
$findme   = 'a';
$pos = strpos($mystring, $findme);

Example 5: strpos in php

<?php
// We can search for the character, ignoring anything before the offset
$newstring = 'abcdef abcdef';
$pos = strpos($newstring, 'a', 1); // $pos = 7, not 0
?>

Tags:

Php Example