[英]Search with PHP in XML <B> Data via <C> tag
First, i know that there are already some questions aubout similar things ( Searching an XML file using PHP Load external xml file? ), but i CAN'T make ANY PHP, so the following (search.php) is what I copy-pasted in PHP:首先,我知道已经有一些关于类似事情的问题( 使用 PHP 加载外部 xml 文件搜索 XML 文件? ),但我不能制作任何 PHP,所以以下 (search.php) 是我复制粘贴的在 PHP 中:
<html>
<head>
<title>Search</title>
</head>
<body>
<?php
$search = $_POST["search"];
$xml=simplexml_load_file("inhaltsverzeichniss.xml") or die("Error: Cannot create object");
$result = $xml->xpath('//array/entry/tags[.="$search"]');
while(list( , $node) = each($result)) {
echo '/array/entry/tags: ',$node,"\n";
}
?>
</body>
Then this is my HTML code (in case someone needs it):然后这是我的 HTML 代码(以防有人需要它):
<form action="search.php" method="post" >
<input type="text" name="search" placeholder="search for stuff" >
<input type="submit" name="search_button" value="Suche" >
</form>
and this is the XML Document I want to search in (inhaltsverzichniss.xml):这是我要搜索的 XML 文档 (inhaltsverzichniss.xml):
<?xml version="1.0" encoding="UTF-8"?>
<array>
<entry>
<url>Zeitformen Spanisch.odp</url>
<name>Zeitformen Tabelle</name>
<tags>spanisch</tags>
</entry>
<entry>
<url>german-stuff.odt</url>
<name>etwas Deutsches</name>
<tags>german</tags>
</entry>
<entry>
<url>something_english.html</url>
<name>english things</name>
<tags>english</tags>
</entry>
<entry>
<url>other-data.fileextension</url>
<name>cool name</name>
<tags>etc.</tags>
</entry>
</array>
What I want to do is search for tags written in the search form, and give the whole corresponding entry out.我想要做的是搜索搜索表单中写的标签,并给出整个相应的条目。 (like following:
<a href"$url">$name</a><br>Tags: $tags
) (如下所示:
<a href"$url">$name</a><br>Tags: $tags
)
EDIT: this is my current XML with entries with multiple tags (changed it to "tag"):编辑:这是我当前的 XML,其中包含多个标签的条目(将其更改为“标签”):
<array>
<entry>
<url>spanisch/Zeitformen_Spanisch.ods</url>
<name>Zeitformen Tabelle</name>
<tag>spanisch</tag>
</entry>
<entry>
<url>german-stuff.odt</url>
<name>etwas Deutsches</name>
<tag>deutsch</tag>
<tag>spanisch</tag>
<tag>englisch</tag>
</entry>
<entry>
<url>something_english.html</url>
<name>english things</name>
<tag>spanisch</tag>
<tag>englisch</tag>
</entry>
<entry>
<url>other-data.fileextension</url>
<name>cool name</name>
<tag>etc.</tag>
</entry>
</array>
Quoting the PHP Manual:引用 PHP 手册:
Note: Unlike the double-quoted and heredoc syntaxes, variables and escape sequences for special characters will not be expanded when they occur in single quoted strings.
注意:与双引号和heredoc 语法不同,特殊字符的变量和转义序列在出现在单引号字符串中时不会被扩展。
You are using single quotes in您正在使用单引号
$result = $xml->xpath('//array/entry/tags[.="$search"]');
but single quotes will treat the string content as literal strings.但单引号会将字符串内容视为文字字符串。 It won't interpolate
$search
like you think it does.它不会像您认为的那样插入
$search
。
Change your code to将您的代码更改为
$result = $xml->xpath("//array/entry/tags[.='$search']");
and it will use the value of $search
instead of the literal string "$search"
.它将使用
$search
的值而不是文字字符串"$search"
。
However, since you want to print the full entry element, it would be better to use this XPath但是,由于您要打印完整的条目元素,因此最好使用此 XPath
$result = $xml->xpath("/array/entry[tags='$search']");
For readability I prefer为了可读性,我更喜欢
$result = $xml->xpath(sprintf('/array/entry[tags="%s"]', $search));
This will give you all the <entry>
elements, instead of the <tags>
elements in the $result
.这将为您提供所有
<entry>
元素,而不是$result
中的<tags>
元素。 And while //array
with a double slash does work, it's unneeded and performs somewhat worse than the direct path.虽然带有双斜杠的
//array
确实有效,但它是不需要的,并且性能比直接路径差一些。 That's because it will try to find the nodes regardless of position in the document.那是因为无论在文档中的位置如何,它都会尝试找到节点。
You can then print it like you asked for like this:然后你可以像你要求的那样打印它:
foreach ($result as $entry) {
printf(
'<a href="%s">%s</a><br>Tags: %s%s',
urlencode($entry->url),
htmlspecialchars($entry->name),
htmlspecialchars($entry->tags),
PHP_EOL
);
}
Regarding your edit with multiple tag elements:关于您对多个标签元素的编辑:
The XPath above will already find the correct entries.上面的 XPath 已经可以找到正确的条目。 To display all tags, you can change the line
要显示所有标签,您可以更改行
htmlspecialchars($entry->tags),
to (don't forget to adjust the xpath to read tag instead of tags):到(不要忘记调整 xpath 以读取标签而不是标签):
implode_tags($entry->tag),
with the implode_tags function being defined as implode_tags 函数被定义为
function implode_tags(\SimpleXMLElement $tags) {
$allTags = [];
foreach ($tags as $tag) {
$allTags[] = htmlspecialchars($tag);
}
return implode(", ", $allTags);
}
This should then return something like "deutsch, spanisch, englisch"这应该返回类似“deutsch, spanisch, englisch”的内容
See full demo at https://eval.in/885485在https://eval.in/885485查看完整演示
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.