String function in php
String
chop() Function
chop() function removes whitespaces or other predefined characters from the right end of a string.
File name : index.php
<?php
$str = "Hello World!";
echo $str . "<br>";
echo chop($str,"World!");
?>
Output :-
File name : index.php
<?php
$str = "Hello World!\n\n";
echo $str;
echo chop($str);
?>
Output :-
Hello World!
Hello World!
str_split() Function
str_split() function splits a string into an array.
Syntax
File name : index.php
str_split(string,length)
File name : index.php
<?php
print_r(str_split("Hello",3));
?>
Output :-
Array ( [0] => Hel [1] => lo )
Generate Random String
File name : index.php
<?php
function generateRandomString($length = 10) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
echo generateRandomString();
?>
Output :-
How to Generate Random String
File name : index.php
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$random_strng = substr(str_shuffle($characters), 0, 4);
trim() function
The trim() function is used to removes whitespace and other predefined characters from both sides of a string in PHP.
File name : index.php
<!DOCTYPE html>
<html>
<body>
<?php
$str = "Hello World!";
echo $str . "<br>";
echo trim($str,"Hello"). "<br>";
?>
</body>
</html>
ltrim() Function
The ltrim() function is used to removes whitespace or other predefined characters from the left side of a string.
File name : index.php
<!DOCTYPE html>
<html>
<body>
<?php
$str = "Students Tutorial";
echo $str . "<br>";
echo ltrim($str,"Students"). "<br>";
?>
</body>
</html>
rtrim() Function
The rtrim() function is used to removes whitespace or other predefined characters from right sides of a string.
File name : index.php
<!DOCTYPE html>
<html>
<body>
<?php
$str = "Students Tutorial";
echo $str . "<br>";
echo rtrim($str,"Tutorial"). "<br>";
?>
</body>
</html>
preg_replace()
The preg_replace() function is use to regular expressions to remove whitespace from a string.
$str = " Hello Sana ";
$str = preg_replace('/\s+/', '', $str);
echo $str;
// Outputs: "Hellosana"
substr() function
The substr() function is used to returns a part of a string
File name : index.php
<!DOCTYPE html>
<html>
<body>
<?php
$name=substr("mahtab habib",5);
echo $name;
?>
</body>
</html>
strlen() function
The strlen() function is used to returns the length of a string
File name : index.php
<!DOCTYPE html>
<html>
<body>
<?php
echo strlen("itech Tutorial");
?>
</body>
</html>
File name : index.php
<!DOCTYPE html>
<html>
<body>
<?php
if(isset($_POST['save']))
{
$name = $_POST['name'];
$email = $_POST['email'];
$password = $_POST['password'];
$count=strlen($password);
if ($count>=6){
echo "";
}
else {
echo "Password length at least 6";
}
}
?>
<form method="post" action="">
Name:<br>
<input type="text" name="name">
<br>
Email Id:<br>
<input type="email" name="email">
<br>Password:<br>
<input type="password" name="password">
<br><br>
<input type="submit" name="save" value="submit">
</form>
</body>
</html>
extract() Function
the extract() function is used to imports variables into the local symbol table from an array.
File name : index.php
<!DOCTYPE html>
<html>
<body>
<?php $a = "Original"; $m_array = array("a" => "itechtuto","b" => "tutorial"); extract($m_array); echo "\$a = $a; \$b = $b;"; ?>
</body>
</html>
strcmp() function
The strcmp() function is used to compares two strings in PHP.
File name : index.php
<?php
$var1 = "Hello";
$var2 = "Hello";
echo strcmp($var1, $var2);
?>
it returns 0, then two strings are equal.
File name : index.php
<?php
$var1 = "Hello";
$var2 = "hello";
echo strcmp($var1, $var2);
?>
If it returns -1, then two strings are not equal.
File name : index.php
<?php
$var1 = "hello";
$var2 = "Hello";
echo strcmp($var1, $var2);
?>
If it returns 1, then two strings are not equal.
addcslashes() Function
the addcslashes() function is used to add backslashes in front of the specified characters.
NoteThe addcslashes() function is case-sensitive.
File name : index.php
<!DOCTYPE html>
<html>
<body>
<?php
$str = "Welcome to students tutorial!";
echo $str."
";
echo addcslashes($str,'s')."
";
echo addcslashes($str,'t')."
";
?>
</body>
</html>
htmlentities() Function
the htmlentities() function used to converts characters to HTML entities
File name : index.php
<!DOCTYPE html>
<html>
<body>
<?php
$str = '<a href="https://www.itechtuto.com">Go to itechtuto.com</a>';
echo htmlentities($str);
?>
</body>
</html>
strrev() Function
the strrev() function is used to get reverses of a string.
File name : index.php
<!DOCTYPE html>
<html>
<body>
<?php echo strrev("Hello World!");
?>
</body>
</html>
chr()
The chr() function returns a character from the specified ASCII value.
$password = chr(rand(65,90)).rand(100,999).chr(rand(97,122)).rand(100,999);
// update this password in forget password case
Previous
Next