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