HMV.co.in

September 28, 2008

Remove blank spaces and new lines from string

Filed under: php — Tags: , , , , , — Harsha M V @ 2:55 am

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));

September 23, 2008

Remove all characters except letters and numbers from a string

Filed under: php — Tags: , , , , — Harsha M V @ 10:57 pm

Description

If you want to strip a string of all symbols and characters other than alphanumeric letters and numbers then use this. It will take a string and erase / delete any non-alphanumeric characters, and then output a clean version without the unwanted characters.

The code


<?php

$string "This is some text and numbers 12345 and symbols !£$%^&";

$new_string ereg_replace("[^A-Za-z0-9]"""$string);

echo $new_string

?>

Blog at WordPress.com.