PHP String Functions

String Functions

Image result for String Functions in php

 strtolower() Function:-

Convert all characters to lowercase:
Syntax:

strtolower(string)
<?php
echo strtolower("Hello WORLD");
?>

strtoupper() Function:-

Convert all characters to uppercase:
Syntax:

strtoupper(string)
<?php
echo strtoupper("Hello WORLD!");
?>

ucfirst() Function:-

Convert the first character of "hello" to uppercase:
Syntax:

ucfirst(string)
<?php
echo ucfirst("hello world!");
?>

lcfirst() Function:-

The lcfirst() function converts the first character of a string to lowercase.
Syntax:

lcfirst(string)
<?php
echo lcfirst("hello world!");
?>

ucwords() Function:-

Convert the first character of each word to uppercase.
 
Syntax:

ucwords(string)
<?php
echo ucwords("hello world");
?>

Strrev() Function:-
The strrev() function reverses a string.
Syntax:

strrev(string)
<?php
echo strrev("Hello World!");
?>

strlen() Function:-

The strlen() function returns the length of a string.
Syntax:

strlen(string)
<?php
echo strlen("Hello");
?>

strpos() Function:-

The strpos() function finds the position of the first occurrence of a string inside another string.

The strpos() function is case-sensitive.
Syntax:
strpos(string,find,start)

string:- Required. Specifies the string to search
find:- Required. Specifies the string to find
start:- Optional. Specifies where to begin the search

strpos() Function
<?php
echo strpos("I love php, I love php
too!","php");
?>

str_replace() Function:-

The str_replace() function replaces some characters with some other
characters in a string.
This function is case-sensitive.
Syntax:

str_replace(find,replace,string,count)

find:- Required. Specifies the value to find
replace:- Required. Specifies the value to replace the value in find
string:- Required. Specifies the string to be searched
count:- Optional. A variable that counts the number of replacements

<?php
$oldstr = "The cat was black";
$newstr = str_replace("black", "white",
$oldstr, $a);
// Displays: The cat was white
echo $newstr;
echo $a;
?>




str_split() Function:-

The str_split() function splits a string into an array.
Syntax:

str_split(string,length)

string:- Required. Specifies the string to split
length:- Optional. Specifies the length of each array element. Default is 1

<?php
print_r(str_split("Hello"));
?>
<?php
$a = str_split("Hello");
print_r($a);
echo $a[0];
?>

str_word_count() Function:-

The str_word_count() function counts the number of words in a string.

Syntax:

str_word_count(string,return,char)

string:- Required. Specifies the string to check
return:- Optional. Specifies the return value of the str_word_count()
function.

Possible values:
0 - Default. Returns the number of words found
1 - Returns an array with the words from the string
2 - Returns an array where the key is the position of the word in the string, and value is the actual word
char Optional. Specifies special characters to be considered as words.

<?php
echo str_word_count("Hello world!");
?>
$a = str_word_count("Hello world!",1);
echo $a[1];

str_word_count() Function:-
<?php
print_r(str_word_count("Hello world!",2));
?>
<?php
print_r(str_word_count("Hello world & good morning!",1));
print_r(str_word_count("Hello world & good
morning!",1,"&"));
?>

strcmp() Function:-

The strcmp() function compares two strings.
Syntax:

strcmp(string1,string2)

string1:-Required. Specifies the first string to compare
string2:-Required. Specifies the second string to compare

Return Value: This function returns:-
0 - if the two strings are equal
<0 - if string1 is less than string2
>0 - if string1 is greater than string2

<?php
echo strcmp("Hello","Hello");
echo "<br>";
echo strcmp("Hello","hELLo");
?>

substr() Function:-

The substr() function returns a part of a string.
Syntax:

substr(string,start,length)
string:- Required. Specifies the string to return a part of
start:- Required. Specifies where to start in the string
Start can be any of following:
A positive number - Start at a specified position in the string
A negative number - Start at a specified position from the end of the
string
0 - Start at the first character in string

length:-
Optional. Specifies the length of the returned string. Default is to the end of the string.

Length can be any of following:

A positive number - The length to be returned from the start
parameter
Negative number - The length to be returned from the end of
the string

<?php
echo substr("Hello world",10)."<br>";
echo substr("Hello world",1)."<br>";
echo substr("Hello world",3)."<br>";
echo substr("Hello world",7)."<br>";
echo substr("Hello world",-1)."<br>";
echo substr("Hello world",-10)."<br>";
echo substr("Hello world",-8)."<br>";
echo substr("Hello world",-4)."<br>";
?>

<?php
echo substr("Hello world",0,10)."<br>";
echo substr("Hello world",1,8)."<br>";
echo substr("Hello world",0,5)."<br>";
echo substr("Hello world",6,6)."<br>";
echo substr("Hello world",0,-1)."<br>";
echo substr("Hello world",-10,-2)."<br>";
echo substr("Hello world",0,-6)."<br>";
?>



strtok() Function:-

The strtok() function splits a string into smaller strings
(tokens).
Syntax:

strtok(string,split)

string:- Required. Specifies the string to split
split:- Required. Specifies one or more split characters

<?php
$string = "Hello world. Beautiful day today.";
$token = strtok($string, " ");
while ($token !== false)
{
echo "$token<br>";
$token = strtok(" ");
}
?>

str_repeat() function:-

The str_repeat() function repeats a string a specified number of times.
Syntax:
str_repeat(string,repeat)

string:- Required. Specifies the string to repeat
repeat:- Required. Specifies the number of times the string will be repeated. Must be greater or equal to 0

<?php
echo str_repeat(".",13);
?>

substr_count() Function:-

- The substr_count() function counts the number of times a substring occurs in a
string.

- The substring is case-sensitive.
- This function does not count overlapped substrings.
- This function generates a warning if the start parameter plus the length parameter is greater than the string length.

Syntax:

substr_count(string,substring,start,length)

string:-Required. Specifies the string to check
substring:- Required. Specifies the string to search for
start:- Optional. Specifies where in string to start searching
length:- Optional. Specifies the length of the search

<?php
$str = "This is nice";
echo strlen($str)."<br>"; // Using strlen() to return the string length
echo substr_count($str,"is")."<br>"; // The number of times "is"
occurs in the string
echo substr_count($str,"is",2)."<br>"; // The string is now reduced
to "is is nice"
echo substr_count($str,"is",3)."<br>"; // The string is now reduced
to "s is nice"
echo substr_count($str,"is",3,3)."<br>"; // The string is now reduced
to "s i"
?>

Post a Comment

1 Comments