PHP String Manipulation

In computer programming, a string is an array or a sequence of characters. String is a basis data-type in PHP. PHP allows us to perform different operations on a string for example reversing words of a string, finding a portion of a string, encrypting a strings etc.

To access a portion of a string

It is possible to access a portion of a string through substr() function.

Example

<?php 
$x = "You are my best friend";
$y = substr($x,9,2);
echo $y;
?> 

This will produce following result

my

To change a portion of a string

It is possible to change a portion through substr_replace() function.

Example

<?php 
$x = "You are my best friend";
$y = substr_replace("my","our",$x);
echo $y;
?> 

This will produce following result

You are our best friend

To find length of a string

It is possible to find the length of a string through strlen() function.

Example

<?php 
$x = "GoodMorning";
y = strlen($x);
echo y;
?> 

This will produce following result

11

To reverse a string

It is possible to reverse the string through strrev() function. It will not change the case of the letters of the string.

Example

<?php 
$x = "You are beautiful";
$y = strrev($x);
echo $y;
?> 

This will produce following result

lufituaeb era uoY

To search for the position of a word in a string

It is possible to search for the position of a word in a string through strpos() function. If it finds the word in the string then the function returns the character position of its first match but If it does not find the word, function returns FALSE.

Example

<?php 
$x = "You are beautiful";
y = strpos($x,"beautiful");
echo y;
?> 

This will produce following result

7

To get word count in a string

It is possible to count number of words present in a string through str_word_count() function.

Example

<?php 
$x = "You are beautiful";
y = str_word_count($x);
echo y;
?> 

This will produce following result

3

To count number of occurrences of a word in a string

It is possible to count number of occurrences of a word in a string through substr_count() function.

Example

<?php 
$x = "You are very very beautiful";
y = substr_count($x,"very");
echo y;
?> 

This will produce following result

2

To search for a set of characters

It is possible to search for a set of characters with strpbrk()function. The function returns the portion of the string from the first matched character to the end of the string. If no character in the set is found in the string, strpbrk() returns false .

Example

<?php 
$x = "You are very very beautiful";
$y = strpbrk( $x, "abcd" );
echo $y;
?> 

This will produce following result

are very very beautiful


SHARE THIS IF YOU LIKE!
submit to reddit

SUBSCRIBE FOR LATEST UPDATES
HAVE WE CONNECTED ON FACEBOOK?
TOP TUTORIALSTOP CODE EXAMPLESTOP LIVE DEMOLINKSSTAY CONNECTED
HTML Tutorial
HTML5 Tutorial
Bootstrap3 Tutorial
Javascript Tutorial
TypeScript Tutorial
AngularJS Tutorial
CSharp Tutorial
PHP Tutorial
Developer Tools
Javascript
AngularJS
Google MAP API V3
ASP.NET
Javascript
AngularJS
Google MAP API V3
Contact Us
Advertise with Us
Privacy Policy
Disclaimer
Stay connected. We actually like you. If you want to get up to the minute updates, then connect with us on your fav below.
 
Twitter   Linkedin   Facebook   Google+    RSS   Pinterest
 
Protected by Copyscape Original Content Checker
 
 
Copyright ©2017 www.techstrikers.com Unauthorized reproduction/replication of any part of this site is prohibited.