Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在XML文件中的节点中搜索文本的子字符串_Php_Xpath - Fatal编程技术网

Php 在XML文件中的节点中搜索文本的子字符串

Php 在XML文件中的节点中搜索文本的子字符串,php,xpath,Php,Xpath,我在一个问答论坛中找到了这个PHP,它查询XML文件: $doc = new DOMDocument; // Create a new dom document $doc->preserveWhiteSpace = false; // Set features $doc->formatOutput = true;

我在一个问答论坛中找到了这个PHP,它查询XML文件:

$doc = new DOMDocument;                                        // Create a new dom document 
$doc->preserveWhiteSpace = false;                              // Set features 
$doc->formatOutput = true;                                     // Create indents on xml 

$doc->Load('i.xml');                                        // Load the file 

$xpath = new DOMXPath($doc); 
$query = '//users/user/firstname[.= "'.$_POST["search"].'"]';         // The xpath (starts from root node) 
$names = $xpath->query($query);                                 // A list of matched elements 


$Output=""; 
foreach ($names as  $node) { 
   $Output.=$doc->saveXML($node->parentNode)."\n";             // We get the parent of  "<firstname>" element  (the entire "<user>" node and its children) (maybe get the parent node directly using xpath) 
                                                                // and use the saveXML() to convert it to string   
} 

echo $Output."<br>\n\n";                                      // The result  

echo "<hr><br><b>Below view the results  as HTML content. &nbsp; (See also the page's HTML code):</b>\n<pre>".htmlspecialchars($Output)."</pre>"; 
脚本将从POST的输入中搜索XML文档中所有firstname节点的值,如果POST输入值与任何节点值匹配,脚本将返回nodefirstname的父节点

这个脚本工作得很好,但它只返回包含firstname节点的整个值的查询,如果我搜索节点文本的子字符串,它将不起作用。例如,对Potato的查询将返回Potato,但对Pot的查询不会给我Potato的结果


那么,如何获得只包含节点文本的子字符串而不是整个值的结果呢?

可能尝试添加wilcard,更改:$query='//users/user/firstname[.='.$\u POST[search].]';to$query='//users/user/firstname[.='.$\u POST[search].*]';它不会返回xpath中带有通配符的任何结果