Tools, FAQ, Tutorials:
Finding a Substring in PHP
How to Find a Substring from a Given String?
✍: FYIcenter.com
To find a substring in a given string, you can use the strpos() function. If you call strpos($haystack, $needle), it will try to find the position of the first occurrence of the $needle string in the $haystack string. If found, it will return a non-negative integer represents the position of $needle. Otherwise, it will return a Boolean false. Here is a PHP script example of strpos():
<?php
$haystack1 = "2349534134345fyicenter16504381640386488129";
$haystack2 = "fyicenter234953413434516504381640386488129";
$haystack3 = "center234953413434516504381640386488129fyi";
$pos1 = strpos($haystack1, "fyicenter");
$pos2 = strpos($haystack2, "fyicenter");
$pos3 = strpos($haystack3, "fyicenter");
print("pos1 = ($pos1); type is " . gettype($pos1) . "\n");
print("pos2 = ($pos2); type is " . gettype($pos2) . "\n");
print("pos3 = ($pos3); type is " . gettype($pos3) . "\n");
?>
This script will print:
pos1 = (13); type is integer pos2 = (0); type is integer pos3 = (); type is boolean
"pos3" shows strpos() can return a Boolean value.
⇒ Testing the strpos() Return Value in PHP
⇐ Removing Leading and Trailing Spaces in PHP
2016-10-13, ∼2413🔥, 0💬
Popular Posts:
What properties and functions are supported on requests.models.Response objects? "requests" module s...
What is EPUB 2.0 Metadata "dc:creator" and "dc:contributor" elements? EPUB 2.0 Metadata "dc:creator"...
How to validate the id_token signature received from Azure AD v2.0 authentication response? You can ...
How to read Atom validation errors at w3.org? If your Atom feed has errors, the Atom validator at w3...
How To Set session.gc_divisor Properly in PHP? As you know that session.gc_divisor is the frequency ...