post processing on google urls

后端 未结 2 1264
挽巷
挽巷 2021-01-28 17:32

I have grabbed some urls from Google search results using a regular expression. It has provided me the links in the format given below. Now, I just want the scheme and the host.

相关标签:
2条回答
  • 2021-01-28 18:12

    Regex to match the above mentioned URL's which are preceded by /url?q= ,

    \/url\?q=\K.*?(?=\/&)
    

    DEMO

    OR

    www\.[^.]*\.(?:org|com)
    

    DEMO

    Your PHP code would be,

    <?php
    $url = <<< 'EOT'
    /url?q=http://www.fertile-focus.com/&sa=U&ei=dTTTU7L2A4egugSY44LgAQ&ved=0CCsQFjAEOGQ&usg=AFQjCNEwG9ntbG0ZtqbqjJNSfVTlqQJYmg
    
    /url?q=http://www.genetests.org/&sa=U&ei=dTTTU7L2A4egugSY44LgAQ&ved=0CDgQFjAGOGQ&usg=AFQjCNFiux9o5YIUGP4P8B_oG_J6iD1Y6g
    EOT;
    $regex =  '~\/url\?q=\K.*?(?=\/&)~';
    preg_match_all($regex, $url, $matches);
    var_dump($matches);
    ?>
    

    Output:

    array(1) {
      [0]=>
      array(2) {
        [0]=>
        string(28) "http://www.fertile-focus.com"
        [1]=>
        string(24) "http://www.genetests.org"
      }
    }
    
    0 讨论(0)
  • 2021-01-28 18:25

    I'd use parse_url and parse_str to achieve this rather than regular expressions.

    <?php
        $url = "/url?q=http://www.fertile-focus.com/&sa=U&ei=dTTTU7L2A4egugSY44LgAQ&ved=0CCsQFjAEOGQ&usg=AFQjCNEwG9ntbG0ZtqbqjJNSfVTlqQJYmg";
        $query = parse_url($url, PHP_URL_QUERY);
        parse_str($query,$result);
        echo $result['q'];
    

    DEMO

    0 讨论(0)
提交回复
热议问题