简体   繁体   English

select 标记 php 并获取 href

[英]select tag in php and get href

i want get all link in page by class "page1" in php .我想通过 class "page1" 在 php 中获取页面中的所有链接 the same code in jquery jquery 中的相同代码

$("a#page1").echo(function()
{
});

can do that in php?可以在 php 中做到这一点吗?

$pattern = '`.*?((http|ftp)://[\w#$&+,\/:;=?@%.-]+)[^\w#$&+,\/:;=?@%.-]*?`i';
preg_match_all($pattern,$page_g,$matches);

this code get all href in the $page_g but its not work for class="page1".此代码获取 $page_g 中的所有 href,但不适用于 class="page1"。 i want only all href in $page_g by class="page1" can help me for optimize reqular ex or other way?我只想要 $page_g 中的所有 href by class="page1"可以帮助我优化常规 ex 或其他方式吗? for example例如

$page_g="<a href="/?s=cache:16001429:office+s01e02" title="" class="big">the <strong>office</strong> us s01 05 xvid mu</a> <a href="asd.com" class="a">asd</a>";

i want return only /?s=cache:16001429:office+s01e02 tnx我只想返回 /?s=cache:16001429:office+s01e02 tnx

You lack the expertise to use a regular expression for that.您缺乏为此使用正则表达式的专业知识。 Hencewhy using DOMdocument is the advisable solution here.因此,为什么使用 DOMdocument 是这里的明智解决方案。 If you want to have a simpler API then use the jQuery-lookalikes phpQuery or QueryPath :如果你想要一个更简单的 API 然后使用 jQuery-lookalikes phpQueryQueryPath

$link = qp($html)->find("a#page1")->attr("href");
print $link;

Edit Edited since you clarified the question.编辑自从您澄清了问题以来已编辑。

To get all <a> links with the class .page1 :要使用 class .page1获取所有<a>链接:

// Load the HTML from a file
$your_HTML_string = file_get_contents("html_filename.html");

$doc = new DOMDocument();
$doc->loadHTML($your_HTML_string);

// Then select all <a> tags under #page1
$a_links = $doc->getElementsByTagName("a");

foreach ($a_links as $link) {
  // If they have more than one class, 
  // you'll need to use (strpos($link->getAttribute("class"), "page1") >=0)
  // instead of == "page1"

  if ($link->getAttribute("class") == "page1") {
    // do something
  }
}

Use DomDocument to parse HTML page, here's a tutorial:使用 DomDocument 解析 HTML 页面,这里有一个教程:

Tutorial教程

DOM is preferred to be used here, as regex is difficult to maintain if underlying HTML changes, besides, DOM can deal with invalid HTML and provides you access to other HTML parsing related tools.这里优先使用 DOM,因为如果底层 HTML 发生变化,正则表达式很难维护,此外,DOM 可以处理无效的 HTML 并为您提供其他 HTML 解析相关工具的访问。

So, assuming that have a file that contains HTML, and you are searching for classes, this could be the way to go:因此,假设有一个包含 HTML 的文件,并且您正在搜索类,这可能是 go 的方式:

$doc = new DOMDocument;
$doc->load(PATH_TO_YOUR_FILE);
//we will use Xpath to find all a containing your class, as a tag can have more than one class and it's just easier to do it with Xpath. 
$xpath = new DOMXpath($doc);
$list = $xpath->query("//a[contains(@class, 'page1')]"); 
foreach ($list as $a_tag) {
    $href = $a_tag->getAttribute('href');
    //do something
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM