如何在打印前检查文本是否包含特定字符(xpath)?
How to check if text contains specific characters before printing (xpath)?
现在我有了这段代码,效果很好:
这会获取 xpath 中的所有内容并打印出来。
<?php
$parent_title = get_the_title( $post->post_parent );
$html_string = file_get_contents('http://www.weburladresshere.com');
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($html_string);
libxml_clear_errors();
$xpath = new DOMXpath($dom);
$values = array();
$row = $xpath->query('myquery');
foreach($row as $value) {
print($value->nodeValue);
}
?>
我需要在代码中插入两件事(如果可能的话):
- 检查内容是否超过x个字符,则不打印。
- 检查content中是否包含http,则不打印。
如果以上两个都是否定的-拿去打印出来。
如果其中一个是肯定的 - 跳过,然后检查同一页上的第二个查询:
$row = $xpath->query('secondquery');
如果这也包含上述之一,则检查第三个查询(来自同一页面),依此类推。
直到匹配为止。
如有任何帮助,我们将不胜感激。
根据我对问题的理解,您想要一种方法来继续 运行 对 DOMDocument
的查询并评估以下条件。
- 如果
nodeValue
的字符串长度低于阈值
- 如果
nodeValue
的字符串不包含"http"
逻辑条件:
- 如果以上两个都为真则回显到屏幕
- 如果其中一个为假,则 运行 下一个子查询
下面是长度为500个字符的代码。我的示例有 3 个条目,它们具有以下字符数:294、98 和 1305。
<?php
/**
* @param $xpath
* @param $xPathQueries
* @param int $iteration
*/
function doXpathQuery($xpath, $xPathQueries, $iteration = 0)
{
// Validate there's no more subquery to go through
if (!isset($xPathQueries[$iteration])) {
return;
}
$runNextIteration = false;
// Run the XPATH subquery
$rows = $xpath->query($xPathQueries[$iteration]);
foreach ($rows as $row) {
$value = trim($row->nodeValue);
$smallerThanLength = (strlen($value) < 500);
// Case insensitive search, might use "http://" for less false positives
$noHttpFound = (stristr($value, 'http') === FALSE);
// Is it smaller than length, and no http found?
if($smallerThanLength && $noHttpFound) {
echo $value;
} else {
// One of them isn't true so run the next query
$runNextIteration = true;
}
}
// Should we do the next query?
if ($runNextIteration) {
$iteration++;
doXpathQuery($xpath, $xPathQueries, $iteration);
}
}
// Commented out this next line because I'm not sure what it does in this context
// $parent_title = get_the_title( $post->post_parent );
// Get all the contents for the URL
$html_string = file_get_contents('https://theeasyapi.com');
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($html_string);
libxml_clear_errors();
$xpath = new DOMXpath($dom);
// Container that will hold all the rows that match the criteria
$values = [];
// An array containing all of the XPATH queries you want to run
$xPathQueries = ['/html/body/div/section', '/html/body/div'];
doXpathQuery($xpath, $xPathQueries);
这将 运行 所有放入 $xPathQueries
的查询,只要查询产生的值的字符串长度超过 500 或找到 'http'。
现在我有了这段代码,效果很好:
这会获取 xpath 中的所有内容并打印出来。
<?php
$parent_title = get_the_title( $post->post_parent );
$html_string = file_get_contents('http://www.weburladresshere.com');
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($html_string);
libxml_clear_errors();
$xpath = new DOMXpath($dom);
$values = array();
$row = $xpath->query('myquery');
foreach($row as $value) {
print($value->nodeValue);
}
?>
我需要在代码中插入两件事(如果可能的话):
- 检查内容是否超过x个字符,则不打印。
- 检查content中是否包含http,则不打印。
如果以上两个都是否定的-拿去打印出来。
如果其中一个是肯定的 - 跳过,然后检查同一页上的第二个查询:
$row = $xpath->query('secondquery');
如果这也包含上述之一,则检查第三个查询(来自同一页面),依此类推。 直到匹配为止。
如有任何帮助,我们将不胜感激。
根据我对问题的理解,您想要一种方法来继续 运行 对 DOMDocument
的查询并评估以下条件。
- 如果
nodeValue
的字符串长度低于阈值 - 如果
nodeValue
的字符串不包含"http"
逻辑条件:
- 如果以上两个都为真则回显到屏幕
- 如果其中一个为假,则 运行 下一个子查询
下面是长度为500个字符的代码。我的示例有 3 个条目,它们具有以下字符数:294、98 和 1305。
<?php
/**
* @param $xpath
* @param $xPathQueries
* @param int $iteration
*/
function doXpathQuery($xpath, $xPathQueries, $iteration = 0)
{
// Validate there's no more subquery to go through
if (!isset($xPathQueries[$iteration])) {
return;
}
$runNextIteration = false;
// Run the XPATH subquery
$rows = $xpath->query($xPathQueries[$iteration]);
foreach ($rows as $row) {
$value = trim($row->nodeValue);
$smallerThanLength = (strlen($value) < 500);
// Case insensitive search, might use "http://" for less false positives
$noHttpFound = (stristr($value, 'http') === FALSE);
// Is it smaller than length, and no http found?
if($smallerThanLength && $noHttpFound) {
echo $value;
} else {
// One of them isn't true so run the next query
$runNextIteration = true;
}
}
// Should we do the next query?
if ($runNextIteration) {
$iteration++;
doXpathQuery($xpath, $xPathQueries, $iteration);
}
}
// Commented out this next line because I'm not sure what it does in this context
// $parent_title = get_the_title( $post->post_parent );
// Get all the contents for the URL
$html_string = file_get_contents('https://theeasyapi.com');
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($html_string);
libxml_clear_errors();
$xpath = new DOMXpath($dom);
// Container that will hold all the rows that match the criteria
$values = [];
// An array containing all of the XPATH queries you want to run
$xPathQueries = ['/html/body/div/section', '/html/body/div'];
doXpathQuery($xpath, $xPathQueries);
这将 运行 所有放入 $xPathQueries
的查询,只要查询产生的值的字符串长度超过 500 或找到 'http'。