PHP String Handling
PHP String
A string is a sequence of characters, like “Hello PHP!”.
<?php
$s = "Hello PHP!";
echo $s;
?>
Run : http://ideone.com/vAUlM5
Hello PHP!
PHP String Functions
Get The Length of a String using strlen()
Function
The PHP strlen()
function returns the length of a string.
<?php
echo strlen("Hello PHP!");
?>
Run : http://ideone.com/GXnHx2
10
Count The Number of Words in a String using word_count()
Function
<?php
echo str_word_count("Hello PHP!");
?>
2
Reverse a String using strrev()
Function
The PHP strrev()
function reverses a string:
<?php
echo strrev("Hello PHP!");
?>
Run : http://ideone.com/E3puUo
!PHP olleH
Search For a Specific Text Within a String using strpos()
Function
The PHP strpos()
function searches for a specific text within a string.
If a match is found, the function returns the character position of the first match. If no match is found, it will return FALSE.
<?php
echo strpos("Hello PHP!", "PHP");
?>
Run : http://ideone.com/daQuxN
6
Replace Text Within a String using str_replace()
Function
The PHP str_replace()
function replaces some characters with some other characters in a string.
<?php
echo str_replace("world","Peter","Hello world!");
?>
Run : http://ideone.com/AFs0DW
Hello Arvin!
Converts String into Uppercase using strtoupper()
Function
This String function converts all letters of String into Uppercase.
<?php
echo strtoupper("it should be in uppercase");
?>
Run : http://ideone.com/Obs3B2
IT SHOULD BE IN UPPERCASE
Converts String into Lowercase using strtolower()
function
This String function converts all letters of String into Uppercase.
<?php
echo strtolower("IT SHOULD BE IN LOWERCASE");
?>
Run : http://ideone.com/kITJOT
it should be in lowercase
Capitalize the first letter of each word using ucwords()
Function
This function is used to capitalized the first letter of each word in String.
<?php
echo ucwords("it should be in uppercase");
?>
Run : http://ideone.com/83vvyF
It Should Be In Uppercase
Remove the Whitespace using trim()
Function
This function is only removing the whitespace at the start and end of the String.
<?php
echo trim(" remove the whitesapce from start and end of the string ");
?>
Run : http://ideone.com/W0gD3v
remove the whitesapce from start and end of the string
Interview Questions & Answers |
---|
No Content Found. |