Instead of a bunch of str_replace calls?

Discussion in 'PHP' started by tarponkeith, Oct 30, 2007.

  1. #1
    I've got a chunk of code like this:

    
    				$new_file = str_replace("-", "_", $new_file);
    				$new_file = str_replace("/", "_", $new_file);
    				$new_file = str_replace("(", "_", $new_file);
    
    PHP:
    It replaces all dashes, forward slashes, and opening parenthesis with an underscore in the variable $new_file...

    Is there a better way to do this?

    I tried:

    
    				$new_file = str_replace("-/(", "_", $new_file);
    
    PHP:
    To no avail.

    Thanks PHP guys...
     
    tarponkeith, Oct 30, 2007 IP
  2. Brewster

    Brewster Active Member

    Messages:
    489
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    60
    #2
    Try this:

    $new_file = str_replace( array( '-', '/', '(' ), "_", $new_file );
    PHP:
    Brew
     
    Brewster, Oct 30, 2007 IP
    tarponkeith likes this.
  3. tarponkeith

    tarponkeith Well-Known Member

    Messages:
    4,758
    Likes Received:
    279
    Best Answers:
    0
    Trophy Points:
    180
    #3
    It looks like it worked, thanks +REP :)
     
    tarponkeith, Oct 30, 2007 IP