Php 如何使用正则表达式从字符串中提取它?
我有一个巨大的链接列表,如下所示:Php 如何使用正则表达式从字符串中提取它?,php,regex,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,51463-Here_Goes_A_Name.aspx,这是名字 如何做到这一点?我可以使用PHP或记事本++ 谢谢 <?php $url="http://www.doma
<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,51463-Here_Goes_A_Name.aspx,这是名字
如何做到这一点?我可以使用PHP或记事本++
谢谢
<?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;?>
您可以使用regex来启动程序,尽管我并不推荐使用它。 使用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
不过,我的问题中有大量的格式列表。这不会同时获得名称
//a[@class=“srTtl2a”]