Remove blank spaces and new lines from string

you can use the preg_replace function to compress a given string and remove spaces and blank lines from the string.

The use of the function is as given below:

preg_replace(“/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/”, “\n”, $string);

where “$string” is the string you want to remove the spaces and blank lines from.

if in case the above code doesn’t work properly you can use the below given user-defined function :

The Function:

function my_preg_rep($content_area){
$content_area=preg_replace(“/[\r\n]*/”, “”, $content_area);
$content_area=preg_replace(“/[\r\n]+/”, “”, $content_area);
return $content_area;
}

Code :

echo my_preg_rep(addslashes($content_area));

thanks,

Sachin (samsami2um@gmail.com)

Advertisements

4 thoughts on “Remove blank spaces and new lines from string

Leave a Reply

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

WordPress.com Logo

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

Google photo

You are commenting using your Google 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