【问题标题】:Get pagination's last page value with php file_get_contents使用 php file_get_contents 获取分页的最后一页值
【发布时间】:2017-10-15 17:08:02
【问题描述】:

我正在寻找一种方法来找到外部网站的最后一个分页值。使用 get_file_contents 我得到以下结果,但我怎样才能在 Next 之前获得值“9”

DOMElement Object ( [tagName] => ul 
[schemaTypeInfo] => [nodeName] => ul 
[nodeValue] => Prev 1 2 3 4 .. 9 Next 
[nodeType] => 1 [parentNode] => (object value omitted) 
[childNodes] => (object value omitted) 
[firstChild] => (object value omitted) [lastChild] => (object value omitted) 
[previousSibling] => [attributes] => (object value omitted) 
[ownerDocument] => (object value omitted) 
[namespaceURI] => [prefix] => [localName] => ul [baseURI] => 
[textContent] => Prev 1 2 3 4 .. 9 Next )

【问题讨论】:

  • 您可能希望在 PHP 中对文本操作进行一些搜索,以帮助您开始。
  • 如果字符串末尾总是有一个Next,您可能希望使用以下正则表达式preg_match()

标签: php arrays file-get-contents


【解决方案1】:

如果我是你,我会先用空格分割 textContent。那么答案就是[Next Key - 1]。

$textContent = " Prev 1 2 3 4 .. 9 10 Next ";
$arr = explode(' ', $textContent);

if ($key = array_search('Next', $arr)) {
echo $arr[$key - 1];  
}
// output : 10

【讨论】:

    【解决方案2】:

    如果字符串末尾总是有一个Next,您可能希望preg_match() 使用以下正则表达式:([0-9]+) Next on nodeValue 键,如下所示:

    $found = preg_match("#([0-9]+) Next#", $domElement['nodeValue'], $matches)
    

    然后if ($found) {} 检查$matches[1] 中的内容

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-04
      • 2022-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-17
      相关资源
      最近更新 更多