regular expression problem

Discussion in 'PHP' started by bumbar, Aug 26, 2010.

  1. #1
    Hallo!

    
    $text = "hare krishna";
    $text = preg_replace('/\W+/', '-', $text);
    
    PHP:
    $text return hare-krishna, as expected with DASH.
    But if I use Cyrillic, the result is not the same, no DASH.

    I tried
    $text = preg_replace('/\w+/ui', '-', $text);
    PHP:
    and it is not yet.

    Can anyone help me how to separate words with a hyphen in Latin and Cyrillic?

    Thanks!
     
    bumbar, Aug 26, 2010 IP
  2. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #2
    If replacing spaces with dashes is what you are looking to achieve, str_replace will work on all languages.
     
    ThePHPMaster, Aug 26, 2010 IP
  3. HuggyEssex

    HuggyEssex Member

    Messages:
    297
    Likes Received:
    4
    Best Answers:
    2
    Trophy Points:
    45
    #3
    Well if there will always be a space why not just use a str_replace?

    Glen
     
    HuggyEssex, Aug 26, 2010 IP
  4. bumbar

    bumbar Active Member

    Messages:
    68
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    #4
    I want to filter invalid characters for words like ()*&^% and so on. I wont only words, separated by DASH.
    I will use the expression for URL address.

    Thanks
     
    bumbar, Aug 26, 2010 IP
  5. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #5
    Without an actual sample of the text and the result you are trying to achieve, It is hard to test which functions you should use. Have a look at the overloaded mb_* functions, maybe one of them can help you:

    http://uk3.php.net/manual/en/mbstring.overload.php
     
    ThePHPMaster, Aug 26, 2010 IP
  6. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #6
    Or you can use u modifier:

    $text = preg_replace('/\W+/mu', '-', $text);
    PHP:
    Variant 2:
    $text = preg_replace('/[^a-zа-я0-9]+/miu', '-', $text);
    PHP:
    Regards :)
     
    koko5, Aug 26, 2010 IP