PHP String functions with examples

Please find below string functions in daily use

trim() : It removes all the whitespaces from both sides of a string,
ltrim() and rtrim() for removing them from only one side of the string (left or right).

It convert string to array based on comma, space etc…
Ex: echo explode(” “,$arr); // Array

It convert array to string
Ex: echo implode(” “,$arr); // String

It’s used for replace word
Ex: echo str_replace(“world”,”Peter”,”Hello world!”);
//world replaced with peter

Perform a regular expression search and replace
Ex: $str = preg_replace(‘/\s+/’, ”, $str);
// But if it could be due to space, tab…you can use

It encode’s the string

Splits a string into an array

Compares two strings (case-sensitive) returns ‘0’ when true.


we are having lot of string functions please find below URL….


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s