What Is String in PHP
What Is String in PHP
What Is String in PHP
<!DOCTYPE html>
<html lang="en">
<head>
<body>
<?php
echo strlen($my_str);
?>
</body>
</html>
Syntax
Strlen(string);
Example
<?php
?>
Output
20
Syntax
Str_word_count(string)
Example
<?php
?>
Output
3
3. Reversing a String
Strrev() is used for reversing a string. You can use this function to get the
reverse version of any string.
Syntax
Strev(string)
Example
<?php
?>
Output
syawduolC ot emocleW
$string = "hitesh";
$length = strlen($string);
?>
Output
hsetih
4. Finding Text Within a String
Strpos() enables searching particular text within a string. It works simply
by matching the specific text in a string. If found, then it returns the
specific position. If not found at all, then it will return “False”. Strops() is
most commonly used in validating input fields like email.
Syntax
Strpos(string,text);
Example
<?php
?>
Output
11
Syntax
Str_replace(string to be replaced,text,string)
Example
<?php
Syntax
Ucwords(string)
Example
<?php
?>
Output
Welcome To The Php World
Syntax
Strtoupper(string);
Example
<?php
Syntax
Strtolower(string)
Example
<?php
?>
Output
welcome to cloudways
9. Repeating a String
PHP provides a built-in function for repeating a string a specific number of
times.
Syntax
Str_repeat(string,repeat)
Example
<?php
echo str_repeat(“=”,13);
?>
Output
=============
Syntax
Strcmp(string1,string2)
Example
<?php
echo strcmp(“Cloudways”,”CLOUDWAYS”);
echo “<br>”;
echo “<br>”;
echo strcmp(“Cloudways”,”Hosting”);
echo “<br>”;
echo “<br>”;
echo strcmp(“abb baa”,”abb baa caa”);//compares both strings and
returns the result in terms of number of characters.
?>
Output
1
-1
-1
-4
Syntax
substr(string,start,length)
Example
<?php
?>
Output
e to Cloudways
Welcome to
Syntax
trim(string,charlist)
Example
<?php
echo trim(“$str”,”Wording”);
?>
Output
Wordpess Hosting
pess Host