Help in regular expression

Discussion in 'PHP' started by kks_krishna, May 18, 2007.

  1. #1
    HI, I want replace the given string. The following are my requirements:

    eg. input string is " Hibernate Interc$%eptors - An Introduction EJB 3.0"

    the output string should be " Hibernate-Interceptors-An-IntroductionEJB-3-0"

    special characters should be removed. please help me.dot shd be replaced with "-".
     
    kks_krishna, May 18, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    
    $text = preg_replace(array('/[^\w-]+/', '/-{2,}/'), array('', '-'), str_replace('.', '-', $text));
    
    PHP:
    This would allow only alphanumeric characters, underscores and dashes. (untested)
     
    nico_swd, May 19, 2007 IP
    commandos likes this.