...

View Full Version : please tell me how to remove these characters from a string?



student
01-28-2007, 02:56 PM
hello,
i am new to php.
please tell me how to remove and all such characters from a string.
there are many such characters i havent written here.
i think it is very difficult to list them all in an array and to use str_replace function etc..
please suggest an easy alternative.

thank you.

GJay
01-28-2007, 03:13 PM
$bad_string = 'some text with some bad characters';
$bad_chars=array('','','','','','');
$good_string=str_replace($bad_chars,'',$bad_string);

http://php.net/str_replace

koyama
01-28-2007, 03:17 PM
...just wondering whether these characters are UTF byte order marks (http://en.wikipedia.org/wiki/Byte_Order_Mark)?

When creating php files, don't use notepad for saving in UTF encoding. It always includes a BOM.

mlseim
01-28-2007, 03:24 PM
Maybe it would be easier to have a list of allowable instead?

$string = "This is a test % of # Special @!+ Characters....";
$filtered = preg_replace('/[^a-z0-9]/i', '', $string);
echo $filtered;

.... the result ...
This is a test of Special Characters

student
01-28-2007, 03:26 PM
$bad_string = 'some text with some bad characters';
$bad_chars=array('','','','','','');
$good_string=str_replace($bad_chars,'',$bad_string);

http://php.net/str_replace

hello GJay and koyama,

these characters are in my mysql database table with some 20,000 rows.
i think there are many such characters.
i would like to delete them all.
so i cant find all such characters and list them in an array.

please suggest an easy method if possible
thank you very much for the replies

student
01-28-2007, 03:28 PM
Maybe it would be easier to have a list of allowable instead?

$string = "This is a test % of # Special @!+ Characters....";
$filtered = preg_replace('/[^a-z0-9]/i', '', $string);
echo $filtered;

.... the result ...
This is a test of Special Characters

thank you very much for the reply.
i will apply it and inform you the result
thank you

student
01-28-2007, 03:50 PM
if i use this code
$filtered = preg_replace('/[^a-z0-9]/i', '', $string);
and dont want to delete '&' symbol from the string, how should i use this function?

thank you

mlseim
01-28-2007, 10:45 PM
<?php
$string = "This is a & test, % of # Special @!+ Characters....";
$filtered = preg_replace('/[^a-z0-9&\s\.\,]/i', '', $string);
echo $filtered;

//Also allow spaces (\s) and ampersand(&) ... and periods and commas too.
?>

student
01-29-2007, 05:01 AM
<?php
$string = "This is a & test, % of # Special @!+ Characters....";
$filtered = preg_replace('/[^a-z0-9&\s\.\,]/i', '', $string);
echo $filtered;

//Also allow spaces (\s) and ampersand(&) ... and periods and commas too.
?>

Hello mlseim,
thank you very much for your reply.
i will implement the code.
thank you again



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum