如何使用正则表达式从字符串中提取?

时间:2014-07-08 10:01:36

标签: php regex

我有很多这样的链接列表:

<a href="http://www.domain.com/main/1,1,51,463-Here_Goes_A_Name.aspx?shmod=1" class="srTtl2a">Here is the name</a>

我想保留 main / 1,1,51,463-Here_Goes_A_Name.aspx 这是名称

怎么做?我可以使用PHP或Notepad ++

谢谢

2 个答案:

答案 0 :(得分:1)

<?php
$url="http://www.domain.com/main/1,1,51,463-Here_Goes_A_Name.aspx?shmod=1";
$host = parse_url($url, PHP_URL_PATH);
echo $host;?>

Output

答案 1 :(得分:1)

你可以使用正则表达式来推动事情,虽然我不会真的推荐它。 使用Bijay Rai提供的parse_url函数,下面的代码完成了这项工作。

PHP示例:

<?php
    $subject = "<a href=\"http://www.domain.com/main/1,1,51,463-Here_Goes_A_Name.aspx?shmod=1\" class=\"srTtl2a\">Here is the name</a><a href=\"http://www.domain.com/main/1,1,51,463-Here_Goes_A_Name.aspx?shmod=1\" class=\"srTtl2a\">Here is the name</a><a href=\"http://www.domain.com/main/1,1,51,463-Here_Goes_A_Name.aspx?shmod=1\" class=\"srTtl2a\">sdasdas</a>";
    $pattern = '/\<a\shref=\"(.+?)\"\s.+?\>(.+?)\<\/a\>/';

    preg_match_all($pattern, $subject, $matches, PREG_SET_ORDER);
    //print_r($matches);

    foreach ($matches as $match) {
        echo "Url: " . $match[1] . "\n";
        echo "Path: " . parse_url($match[1], PHP_URL_PATH) . "\n";
        echo "Title: " . $match[2] . "\n\n";
    }
?>

<强>输出:

Url: http://www.domain.com/main/1,1,51,463-Here_Goes_A_Name.aspx?shmod=1
Path: /main/1,1,51,463-Here_Goes_A_Name.aspx
Title: Here is the name

Url: http://www.domain.com/main/1,1,51,463-Here_Goes_A_Name.aspx?shmod=1
Path: /main/1,1,51,463-Here_Goes_A_Name.aspx
Title: Here is the name

Url: http://www.domain.com/main/1,1,51,463-Here_Goes_A_Name.aspx?shmod=1
Path: /main/1,1,51,463-Here_Goes_A_Name.aspx
Title: sdasdas

Ideone Example