Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder student's Avatar
    Join Date
    Jan 2007
    Posts
    129
    Thanks
    13
    Thanked 0 Times in 0 Posts

    please tell me how to remove these characters from a string?

    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.

  • #2
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    PHP Code:
    $bad_string 'some text¿ with some ® bad characters»';
    $bad_chars=array('ï','»','¿','â','¢','®');
    $good_string=str_replace($bad_chars,'',$bad_string); 
    http://php.net/str_replace
    My thoughts on some things: http://codemeetsmusic.com
    And my scrapbook of cool things: http://gjones.tumblr.com

  • #3
    Senior Coder koyama's Avatar
    Join Date
    Dec 2006
    Location
    Copenhagen, Denmark
    Posts
    1,246
    Thanks
    1
    Thanked 5 Times in 5 Posts
    ...just wondering whether these characters are UTF byte order marks?

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

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,465
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    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

  • #5
    Regular Coder student's Avatar
    Join Date
    Jan 2007
    Posts
    129
    Thanks
    13
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by GJay View Post
    PHP Code:
    $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

  • #6
    Regular Coder student's Avatar
    Join Date
    Jan 2007
    Posts
    129
    Thanks
    13
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by mlseim View Post
    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

  • #7
    Regular Coder student's Avatar
    Join Date
    Jan 2007
    Posts
    129
    Thanks
    13
    Thanked 0 Times in 0 Posts
    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

  • #8
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,465
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    <?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.
    ?>

  • #9
    Regular Coder student's Avatar
    Join Date
    Jan 2007
    Posts
    129
    Thanks
    13
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by mlseim View Post
    <?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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •