Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    ubh
    ubh is offline
    Regular Coder ubh's Avatar
    Join Date
    Apr 2008
    Location
    Portland, Oregon U.S.A.
    Posts
    443
    Thanks
    108
    Thanked 15 Times in 14 Posts

    str_replace regex

    Ok I have a url http://www.something.com/lots/of/stuff/out/here/

    I wan to use str_replace to remove everything after the .com leaving me with:

    http://www.something.com

    I just for the life of me cant remember the regex that I would use in conjunction with the str_replace to do such a thing.. stupid JS has me all confused cause with JS its

    .replace(/.com(.*)/g,"");

    but (.*) is nothing PHP will know what to do with.

    Please a little help?

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    .* is pcre pattern for any character any number of times. You need to use preg_replace if you want to use the pattern.
    And you can use parse_url to do what you're looking to do much easier. It will actually work with any extension though, so you would need more code if you wanted it to be bound to only .com:
    PHP Code:
    $inputUrl =  'http://www.something.com/lots/of/stuff/out/here/';
    $aUrlParts parse_url($inputUrl);
    print_r($aUrlParts); 
    Should be:
    Code:
    Array
    (
        [scheme] => http
        [host] => something
        [user] => 
        [pass] => 
        [path] => /lots/of/stuff/out/here/
        [query] => 
        [fragment] => 
    )
    I believe every part is returned regardless of if its null or not.
    Edit:
    Now that I think of it, it won't return empty parts. On top of that though, this won't work because it don't return the domain suffix.
    So, you can use preg_replace if you want (the pattern you have will actually work), or you can look at using substr/substr_replace and strpos to replace after .com happens.
    Edit:
    Actually, I think it does return the .com as a part of the host, the api is unclear if this is the situation, and I'm at work so I can't test this out.

    Last edited by Fou-Lu; 11-05-2009 at 01:12 AM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    ubh (11-05-2009)

  • #3
    ubh
    ubh is offline
    Regular Coder ubh's Avatar
    Join Date
    Apr 2008
    Location
    Portland, Oregon U.S.A.
    Posts
    443
    Thanks
    108
    Thanked 15 Times in 14 Posts
    Fou-Lu you rock my friend always a huge helper!!

    Thanks m8!

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    np mate, hope you caught my last edit there. Methinks that will work fine.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #5
    ubh
    ubh is offline
    Regular Coder ubh's Avatar
    Join Date
    Apr 2008
    Location
    Portland, Oregon U.S.A.
    Posts
    443
    Thanks
    108
    Thanked 15 Times in 14 Posts
    Got it.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •