Help with preg_replace and encoding

Discussion in 'PHP' started by Philopoemen, May 31, 2008.

  1. #1
    Hey all,

    On some of my websites I have a function that clears all special chars from a string.
    What it does it leaves only 0-9a-zA-Z, and replaces all white spaces with '-'.

    Here it is:
    $string = preg_replace('/[^\-0-9a-zA-Z\/]/', '', str_replace(' ', '-', $string));
    Code (markup):
    The problem is, the strings come from a DataBase, where they are stored in UTF8. Some strings contain special chars that I want to be replaced and not removed.

    For example: "â" and "ă" to be replaced with "a", "ş" with "s" respectively.

    I played around with it for a long time, but I wasn't able to figure this problem out. Can somebody help me out? Rep will be given :)
     
    Philopoemen, May 31, 2008 IP
  2. cornetofreak

    cornetofreak Peon

    Messages:
    170
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #2
    if you enable the mbstring and mbregex, you can use the ereg_ functions
    with regular expressions and UTF-8 strings. preg_ functions are not ever
    UTF-8 enabled as far as i'm aware ...
     
    cornetofreak, May 31, 2008 IP