PHP String Functions - All necessary String functions in PHP to manage strings better.
Categories - PHP Tags - PHP   Maniruzzaman Akash   4 months ago   895   0

PHP String Functions - All necessary String functions in PHP to manage strings better.

PHP String Functions

PHP has several built in methods to work with Strings.
Our Discussed string functions -

  1. ucfirst, ucwords, lcfirst, strtolower, strtoupper,
  2. trim, rtrim, ltrim, strlen
  3. substr, substr_replace, strstr
  4. strpos, strrpos, strripos
  5. str_split
  6. echo , print, printf, sprintf
  7. implode, explode
  8. md5 Hash, sha1 Hash
  9. parse_str
  10. strcmp
  11. strrev
  12. str_shuffle
    Let’s get the details about each functions-
// Add $name variable with this value
$name = "jhon Doe jhon ";

ucfirst - Make a string’s first character uppercase

echo ucfirst($name); // Jhon Doe jhon
echo '<br>';

ucwords - Make each word’s first character uppercase

echo ucwords($name); // Jhon Doe Jhon
echo '<br>';

lcfirst - Make a string’s first character lowercase

echo lcfirst($name); // jhon Doe jhon
echo '<br>';

strtolower - Make a string’s all characters lowercase

echo strtolower($name); // jhon doe jhon
echo '<br>';

strtoupper - Make a string’s all characters uppercase

echo strtoupper($name); // JHON DOE JHON
echo '<br>';

trim, rtrim, ltrim, strlen

trim - Strip whitespace (or other characters) from the beginning and end of a string

var_dump(trim($name)); // string(13) "jhon Doe jhon"
echo '<br>';

rtrim - Strip whitespace (or other characters) from the end of a string

var_dump(rtrim($name)); // string(13) "jhon Doe jhon"
echo '<br>';

ltrim - Strip whitespace (or other characters) from the beginning of a string

var_dump(ltrim($name)); // string(14) "jhon Doe jhon "
echo '<br>';

strlen - Find the length of a string

var_dump(strlen($name)); // int(14)
echo '<br>';

substrsubstr, substr_replace, strstr

substr - Return part of a string

var_dump(substr($name, 0, 5)); // string(5) "jhon "
echo '<br>';

substr_replace - Replace text within a portion of a string

var_dump(substr_replace($name, 'jhon', 0, 10)); // string(8) "jhonhon "
echo '<br>';

strstr - Find the first occurrence of a string

var_dump(strstr($name, "Doe")); // string(9) "Doe jhon "
echo '<br>';
var_dump(strstr($name, "Jhon")); // bool(false)
echo '<br>';

strpos, strrpos, strripos

strpos - Find the position of the first occurrence of a substring in a string

var_dump(strpos($name, "Doe")); // int(5)
echo '<br>';
var_dump(strpos($name, "doe")); // bool(false)
echo '<br>';
var_dump(strpos($name, "jhon ")); // int(0)
echo '<br>';

strrpos - Find the position of the last occurrence of a substring in a string

var_dump(strrpos($name, "jhon d ")); // bool(false)
echo '<br>';

// Looking for 'D' from the nth byte
var_dump(strrpos($name, "Doe", 1)); // int(5)
echo '<br>';
var_dump(strrpos($name, "Doe", 6)); // bool(false)
echo '<br>';

strpos - Find position of last occurrence of a case-insensitive string in a string

var_dump(strpos($name, "doe", 1)); // bool(false)
echo '<br>';
var_dump(strripos($name, "doe", 1)); // int(5)
echo '<br>';

str_split - Convert a string to an array

var_dump(str_split($name)); // array(14) { [0]=> string(1) "j" [1]=> string(1) "h" [2]=> string(1) "o" [3]=> string(1) "n" [4]=> string(1) " " [5]=> string(1) "D" [6]=> string(1) "o" [7]=> string(1) "e" [8]=> string(1) " " [9]=> string(1) "j" [10]=> string(1) "h" [11]=> string(1) "o" [12]=> string(1) "n" [13]=> string(1) " " }
echo '<br>';

echo , print, printf, sprintf

printf - Output a formatted string

printf("Hello %s", $name); // Hello Akash
echo '<br>';

printf("Hello %s", $name);
echo '<br>';

sprintf - Return a formatted string

$data = sprintf("Hello %s", $name);
echo $data;
echo '<br>';

implode, explode

explode - Split a string by a string

var_dump(explode(" ", $name)); // array(4) { [0]=> string(4) "jhon" [1]=> string(3) "Doe" [2]=> string(4) "jhon" [3]=> string(0) "" }
echo '<br>';

implode - Join array elements with a string

$string_array = explode(" ", $name);
var_dump(implode(" ", $string_array));
echo '<br>';

md5 Hash, sha1 Hash

md5 - Calculate the md5 hash of a string (32)

echo md5($name); // f9712d6ccb57ff3cb10bfbb5ef9e2714
echo '<br>';
echo strlen(md5($name)); // 32
echo '<br>';

sha1 - Calculate the sha1 hash of a string (40)

echo sha1($name); // 601441f9f63fd74c0ee2344363b78b75f097fcb6
echo '<br>';

parse_str - Parse a query string into variables

$query = "name=Akash&age=20";
parse_str($query, $stored);
print_r($stored); // Array ( [name] => Akash [age] => 20 )
echo '<br>';

Binary safe string comparison

less 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal.

echo strcmp($name, "Jhon");
echo '<br>';

strrev - Reverse a string

echo strrev($name); //  nohj eoD nohj
echo '<br>';

str_shuffle - Randomly shuffle a string

echo str_shuffle($name); // eno jhoho j nD
echo '<br>';

Full Example

<?php

$name = "jhon Doe jhon ";

/* ucfirst, ucwords, lcfirst, strtolower, strtoupper */
// ucfirst - Make a string's first character uppercase
echo ucfirst($name); // Jhon Doe jhon
echo '<br>';

// ucwords - Make each word's first character uppercase
echo ucwords($name); // Jhon Doe Jhon
echo '<br>';

// lcfirst - Make a string's first character lowercase
echo lcfirst($name); // jhon Doe jhon
echo '<br>';

// strtolower - Make a string's all characters lowercase
echo strtolower($name); // jhon doe jhon
echo '<br>';

// strtoupper - Make a string's all characters uppercase
echo strtoupper($name); // JHON DOE JHON
echo '<br>';


/* trim, rtrim, ltrim, strlen */
// trim - Strip whitespace (or other characters) from the beginning and end of a string
var_dump(trim($name)); // string(13) "jhon Doe jhon"
echo '<br>';

// rtrim - Strip whitespace (or other characters) from the end of a string
var_dump(rtrim($name)); // string(13) "jhon Doe jhon"
echo '<br>';

// ltrim - Strip whitespace (or other characters) from the beginning of a string
var_dump(ltrim($name)); // string(14) "jhon Doe jhon "
echo '<br>';

// strlen - Find the length of a string
var_dump(strlen($name)); // int(14)
echo '<br>';


/* substrsubstr, substr_replace, strstr */
// substr - Return part of a string
var_dump(substr($name, 0, 5)); // string(5) "jhon "
echo '<br>';

// substr_replace - Replace text within a portion of a string
var_dump(substr_replace($name, 'jhon', 0, 10)); // string(8) "jhonhon "
echo '<br>';

// strstr - Find the first occurrence of a string
var_dump(strstr($name, "Doe")); // string(9) "Doe jhon "
echo '<br>';
var_dump(strstr($name, "Jhon")); // bool(false)
echo '<br>';

/* strpos, strrpos, strripos */
// strpos - Find the position of the first occurrence of a substring in a string
var_dump(strpos($name, "Doe")); // int(5)
echo '<br>';
var_dump(strpos($name, "doe")); // bool(false)
echo '<br>';
var_dump(strpos($name, "jhon ")); // int(0)
echo '<br>';

// strrpos - Find the position of the last occurrence of a substring in a string
var_dump(strrpos($name, "jhon d ")); // bool(false)
echo '<br>';
// Looking for 'D' from the nth byte
var_dump(strrpos($name, "Doe", 1)); // int(5)
echo '<br>';
var_dump(strrpos($name, "Doe", 6)); // bool(false)
echo '<br>';

// strpos - Find position of last occurrence of a case-insensitive string in a string
var_dump(strpos($name, "doe", 1)); // bool(false)
echo '<br>';
var_dump(strripos($name, "doe", 1)); // int(5)
echo '<br>';


/* str_split */
// str_split - Convert a string to an array
var_dump(str_split($name)); // array(14) { [0]=> string(1) "j" [1]=> string(1) "h" [2]=> string(1) "o" [3]=> string(1) "n" [4]=> string(1) " " [5]=> string(1) "D" [6]=> string(1) "o" [7]=> string(1) "e" [8]=> string(1) " " [9]=> string(1) "j" [10]=> string(1) "h" [11]=> string(1) "o" [12]=> string(1) "n" [13]=> string(1) " " }
echo '<br>';

/* echo , print, printf, sprintf */
// printf - Output a formatted string
printf("Hello %s", $name); // Hello Akash
echo '<br>';

printf("Hello %s", $name);
echo '<br>';

// sprintf - Return a formatted string
$data = sprintf("Hello %s", $name);
echo $data;
echo '<br>';


/* implode, explode */
// explode - Split a string by a string
var_dump(explode(" ", $name)); // array(4) { [0]=> string(4) "jhon" [1]=> string(3) "Doe" [2]=> string(4) "jhon" [3]=> string(0) "" }
echo '<br>';

// implode - Join array elements with a string
$string_array = explode(" ", $name);
var_dump(implode(" ", $string_array));
echo '<br>';

/* md5 Hash, sha1 Hash */
// md5 - Calculate the md5 hash of a string (32)
echo md5($name); // f9712d6ccb57ff3cb10bfbb5ef9e2714
echo '<br>';
echo strlen(md5($name)); // 32
echo '<br>';

// sha1 - Calculate the sha1 hash of a string (40)
echo sha1($name); // 601441f9f63fd74c0ee2344363b78b75f097fcb6
echo '<br>';

/* parse_str */
// parse_str - Parse a query string into variables
$query = "name=Akash&age=20";
parse_str($query, $stored);
print_r($stored); // Array ( [name] => Akash [age] => 20 )
echo '<br>';

/* strcmp */
// Binary safe string comparison
// less 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal.
echo strcmp($name, "Jhon");
echo '<br>';

/* strrev */
// strrev - Reverse a string
echo strrev($name); //  nohj eoD nohj
echo '<br>';

/* str_shuffle */
// str_shuffle - Randomly shuffle a string
echo str_shuffle($name); // eno jhoho j nD
echo '<br>';
Previous
PHP Math Functions - pi, abs, sqrt, min, max, rand, round, floor, ceil, pow, is_nan, is_numeric
Next
Loops in PHP - for, while, foreach, do-while