Get Major Training|Project Training|Live Project|Industrial Training On Android| I-Phone | Java | J2SE | J2EE | C#.Net | ASP.Net| PHP | PHP with Wordpress | Joomla | Majento Contact us

Friday

How to Replace Special Characters in PHP

Hi

When we what to do things like 

I have a string like $string = "this is my string"; the output should be "thisismystring" or
Or "this_is_my_string"


I want to  Replace white Space or Space or " " from text in php 

or just replace white space with underscore ??

or replace any Special character from text 
How can i do that?


Answer is

For just spaces, use str_replace:
Syntax:
str_replace('Character to replace' ,'Replace by','Source String');

$string = str_replace(' ', '', $string);
For all whitespace, use preg_replace:

preg_replace('Character to replace' ,'Replace by','Source String');

/\s+/ used for all type White Space; 
 
$string = preg_replace('/\s+/', '', $string);
 
 
 
Author: Vikas Agrawal
Vikas Agrawal is a CEO and Founder Of Vinayak Informatics Systems. Read More →

You Like It, Please Share This Article Using...



No comments:

Post a Comment

Please leave your comments and we will be reply you back ASAP
vikas agrawal