Home >> FAQs/Tutorials >> PHP Script Tutorials and Tips >> Index

PHP Script Tips - PHP Built-in Functions for Strings

By: FYICenter.com

Part:   1  2   3  4  5 

(Continued from previous part...)

How to Find a Substring from a Given String?

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. Othewise, 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.

What Is the Best Way to Test the strpos() Return Value?

Because strpos() could two types of values, Integer and Boolean, you need to be careful about testing the return value. The best way is to use the "Identical(===)" operator. Do not use the "Equal(==)" operator, because it does not differentiate "0" and "false". Check out this PHP script on how to use strpos():

<?php
$haystack = "needle234953413434516504381640386488129";
$pos = strpos($haystack, "needle");
if ($pos==false) {
  print("Not found based (==) test\n");
} else {
  print("Found based (==) test\n");
}
if ($pos===false) {
  print("Not found based (===) test\n");
} else {
  print("Found based (===) test\n");
}
?>

This script will print:

Not found based (==) test
Found based (===) test

Of course, (===) test is correct.

How To Take a Substring from a Given String?

If you know the position of a substring in a given string, you can take the substring out by the substr() function. Here is a PHP script on how to use substr():

<?php
$string = "beginning";
print("Position counted from left: ".substr($string,0,5)."\n");
print("Position counted form right: ".substr($string,-7,3)."\n");
?>

This script will print:

Position counted from left: begin
Position counted form right: gin

substr() can take negative starting position counted from the end of the string.

How To Replace a Substring in a Given String?

If you know the position of a substring in a given string, you can replace that substring by another string by using the substr_replace() function. Here is a PHP script on how to use substr_replace():

<?php
$string = "Warning: System will shutdown in NN minutes!";
$pos = strpos($string, "NN");
print(substr_replace($string, "15", $pos, 2)."\n");
sleep(10*60);
print(substr_replace($string, "5", $pos, 2)."\n");
?>

This script will print:

Warning: System will shutdown in 15 minutes!
(10 minutes later)
Warning: System will shutdown in 5 minutes!

Like substr(), substr_replace() can take negative starting position counted from the end of the string.

(Continued on next part...)

Part:   1  2   3  4  5 


Selected Developer Jobs:

More...