Someone help with this function please

Discussion in 'PHP' started by JEET, Apr 16, 2008.

  1. #1
    Hi,
    I need a function to replace anything that is not "alphanumeric" to a _ sign in a string.
    $str= "this is my text!";
    output can be: "This_is_my_text_";
    Please give me a function to do this...
    Thanks :)
     
    JEET, Apr 16, 2008 IP
    hogan_h likes this.
  2. hogan_h

    hogan_h Peon

    Messages:
    199
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I don't know if it's 100% reliable, but you could try this:

    
    $str= "this is my text!";
    
    $result = preg_replace('/[^A-Za-z0-9]/', "_", $str);
    
    echo $result;
    
    PHP:
     
    hogan_h, Apr 16, 2008 IP
    JEET likes this.