How to simplify my trible search and replace

Discussion in 'PHP' started by bobby9101, Mar 24, 2007.

  1. #1
    I am using:
    
    $url = "http://" . str_replace("https://", "", str_replace("http://", "", str_replace("www.", "", $passedurl )));
    
    PHP:
    To clean up URLs, that may I can make sure it is an actual URL
    A. is this going to be a good check
    B. How can I clean it up so that I am not doing a triple search and replace?
     
    bobby9101, Mar 24, 2007 IP
  2. sea otter

    sea otter Peon

    Messages:
    250
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #2
    php has a function called parse_url which breaks a url into its constituent parts and stores them in an array. You can then examine/replace any part you want, and recombine whichever pieces you want to keep into a string.

    Far better to rely on php for this than to try and split it yourself.

    man page here: php.net/manual/en/function.parse-url.php
     
    sea otter, Mar 24, 2007 IP