2013-02-18 85 views
-3

我有html文件和链接,像http://example.com/some?b=20130218082149&a=20130218092249&info=1152&hash=079caaaae2fcd602c6d8dx正则表达式,抢链接

我需要一个正则表达式来找到它,如果你能解释一下表情给我,那会是很有益的。

+2

[StackOverflow上(http://www.stackoverflow.com)是不是一个'你好all'话题制造者,作为域名显示是什么一回事。你需要有人为你做?你必须先尝试一些东西,然后也许有人会指出你的错误。 – 2013-02-18 09:02:42

回答

0

我刺它

<?php 

$pattern = "#^https?://([a-z0-9-]+\.)*blah\.com(/.*)?$#"; 

$tests = array(
    'http://blah.com/so/this/is/good' 
    , 'http://blah.com/so/this/is/good/index.html' 
    , 'http://www.blah.com/so/this/is/good/mice.html#anchortag' 
    , 'http://anysubdomain.blah.com/so/this/is/good/wow.php' 
    , 'http://anysubdomain.blah.com/so/this/is/good/wow.php?search=doozy' 
    , 'http://any.sub-domain.blah.com/so/this/is/good/wow.php?search=doozy' // I added this case 
    , 'http://999.sub-domain.blah.com/so/this/is/good/wow.php?search=doozy' // I added this case 
    , 'http://obviousexample.com' 
    , 'http://bbc.co.uk/blah.com/whatever/you/get/the/idea' 
    , 'http://blah.com.example' 
    , 'not/even/a/blah.com/url' 
); 

foreach ($tests as $test) 
{ 
    if (preg_match($pattern, $test)) 
    { 
    echo $test, " <strong>matched!</strong><br>"; 
    } else { 
    echo $test, " <strong>did not match.</strong><br>"; 
    } 
} 

// Here's another way 
echo '<hr>'; 
foreach ($tests as $test) 
{ 
    if ($filtered = filter_var($test, FILTER_VALIDATE_URL)) 
    { 
    $host = parse_url($filtered, PHP_URL_HOST); 
    if ($host && preg_match("/blah\.com$/", $host)) 
    { 
     echo $filtered, " <strong>matched!</strong><br>"; 
    } else { 
     echo $filtered, " <strong>did not match.</strong><br>"; 
    } 
    } else { 
    echo $test, " <strong>did not match.</strong><br>"; 
    } 
} 
+0

非常感谢。 – 2013-02-18 09:21:53