PHP
PHP String Functions - All necessary String functions in PHP to manage strings better.
- PHP String Functions
- trim, rtrim, ltrim, strlen
- substrsubstr, substr_replace, strstr
- strpos, strrpos, strripos
- echo , print, printf, sprintf
- implode, explode
- md5 Hash, sha1 Hash
¶PHP String Functions
PHP has several built in methods to work with Strings.
Our Discussed string functions -
- ucfirst, ucwords, lcfirst, strtolower, strtoupper,
- trim, rtrim, ltrim, strlen
- substr, substr_replace, strstr
- strpos, strrpos, strripos
- str_split
- echo , print, printf, sprintf
- implode, explode
- md5 Hash, sha1 Hash
- parse_str
- strcmp
- strrev
- 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
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
Loops in PHP - for, while, foreach, do-while
Advertisements
All Tutorials in this playlist
Popular Tutorials
Categories
-
Artificial Intelligence (AI)
11
-
Bash Scripting
1
-
Bootstrap CSS
0
-
C Programming
14
-
C#
0
-
ChatGPT
1
-
Code Editor
2
-
Computer Engineering
3
-
CSS
28
-
Data Structure and Algorithm
18
-
Design Pattern in PHP
2
-
Design Patterns - Clean Code
1
-
E-Book
1
-
Git Commands
1
-
HTML
19
-
Interview Prepration
2
-
Java Programming
0
-
JavaScript
12
-
Laravel PHP Framework
37
-
Mysql
1
-
Node JS
1
-
Online Business
0
-
PHP
28
-
Programming
8
-
Python
12
-
React Js
19
-
React Native
1
-
Redux
2
-
Rust Programming
15
-
Tailwind CSS
1
-
Typescript
10
-
Uncategorized
0
-
Vue JS
1
-
Windows Operating system
1
-
Woocommerce
1
-
WordPress Development
2
Tags
- Artificial Intelligence (AI)
- Bash Scripting
- Business
- C
- C Programming
- C-sharp programming
- C++
- Code Editor
- Computer Engineering
- CSS
- Data Structure and Algorithm
- Database
- Design pattern
- Express JS
- git
- Git Commands
- github
- HTML
- Java
- JavaScript
- Laravel
- Mathematics
- MongoDB
- Mysql
- Node JS
- PHP
- Programming
- Python
- React Js
- Redux
- Rust Programming Language
- TypeScript
- Vue JS
- Windows terminal
- Woocommerce
- WordPress
- WordPress Plugin Development
Advertisements