Removing certain text with PHP?

Discussion in 'PHP' started by misterdh, Aug 11, 2010.

  1. #1
    Hi

    I want this URL:

    php.net/manual/en/function.explode.php

    to become

    php.net

    using php ..

    I tried with the explode fuction but couldnt get it to work ...
    list ($finalurl, $blabla) = explode('/', $url);
    PHP:
    Anyone have any tips?

    Thanks in advance :)
     
    misterdh, Aug 11, 2010 IP
  2. definitely

    definitely Well-Known Member

    Messages:
    520
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    160
    #2
    Try it out...
    http://ishoot.in/create/new.php
    exact code pasted here...

    <?php
    $url="php.net/manual/en/function.explode.php";
    $new=explode('/',$url);
    echo $new[0];
    ?>
    Code (markup):
     
    definitely, Aug 11, 2010 IP
  3. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #3
    <?php
    
    $url = 'php.net/manual/en/function.explode.php';
    
    echo strtok($url, '/');
    
    ?>
    PHP:
     
    danx10, Aug 11, 2010 IP
  4. sea otter

    sea otter Peon

    Messages:
    250
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #4
    To make explode work the way you're using it (returning the data into two variables) you need to add a third parameter to explode limiting the output to only two parts:

    
         list ($finalurl, $blabla) = explode('/', $url, 2);
    
    PHP:
     
    sea otter, Aug 11, 2010 IP
  5. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #5
    nico_swd, Aug 12, 2010 IP
  6. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #6
    That would require http:// before it whereas the OP's example suggests the $url doesn't start with http://

    I guess that can be added though:

    <?php
    
    $url = 'php.net/manual/en/function.explode.php';
    
    if (!preg_match('~^http://~i')) $url = 'http://'.$url;
    
    echo parse_url($url, PHP_URL_HOST);
    
    ?>
    PHP:
     
    danx10, Aug 12, 2010 IP
  7. misterdh

    misterdh Peon

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thanks guys :D
     
    misterdh, Aug 12, 2010 IP