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

Tutorials Class - Output Window

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

Tutorials Class - Output Window

10


Count The Number of Words in a String using word_count() Function

<?php
echo str_word_count("Hello PHP!");
?>

Run: http://ideone.com/afYdZy

Tutorials Class - Output Window

2


Reverse a String using strrev() Function

The PHP strrev() function reverses a string:

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

Run : http://ideone.com/E3puUo

Tutorials Class - Output Window

!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

Tutorials Class - Output Window

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

Tutorials Class - Output Window

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

Tutorials Class - Output Window

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

Tutorials Class - Output Window

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

Tutorials Class - Output Window

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

Tutorials Class - Output Window

remove the whitesapce from start and end of the string


Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
Write a PHP program to reverse the string
Write a PHP program to find the length of the string
Write a PHP program to count the words in the string
Write a PHP program to convert a string into uppercase
Interview Questions & Answers
No Content Found.