在XML文件中搜索多个元素(Searching Multiple Elements In XML File)
我正在使用此处的W3C Live Ajax搜索。 他们的代码只搜索一个元素,即“标题”。 我想让用户的查询和搜索多个元素,例如'title'和'url'。
<?php $xmlDoc=new DOMDocument(); $xmlDoc->load("live.xml"); $x=$xmlDoc->getElementsByTagName('link'); //get the q parameter from URL $q=$_GET["q"]; //lookup all links from the xml file if length of q>0 if (strlen($q)>0) { $hint=""; for($i=0; $i<($x->length); $i++) { $d=$x->item($i)->getElementsByTagName('title'); $z=$x->item($i)->getElementsByTagName('url'); if ($d->item(0)->nodeType==1) { //find a link matching the search text if (stristr($d->item(0)->childNodes->item(0)->nodeValue,$q) !=false) { if ($hint=="") { $hint= $z->item(0)->childNodes->item(0)->nodeValue . "<br />" . $d->item(0)->childNodes->item(0)->nodeValue; } else { $hint=$hint . "<br /><br />" . $z->item(0)->childNodes->item(0)->nodeValue . "<br />" . $d->item(0)->childNodes->item(0)->nodeValue; } } } } } // Set output to "no suggestion" if no hint were found // or to the correct values if ($hint=="") { $response="no suggestion"; } else { $response=$hint; } //output the response echo $response; ?>
有人能给我一个如何让它工作的例子吗? 根据我的理解,stristr只搜索一个haystack,你不能使用数组,那么还有另一种方法吗? 提前致谢!
I'm using the W3C Live Ajax Search found here. Their code only searches one element, the "title". I would like to have it take the user's query and search through multiple elements, for example, the 'title' and the 'url'.
<?php $xmlDoc=new DOMDocument(); $xmlDoc->load("live.xml"); $x=$xmlDoc->getElementsByTagName('link'); //get the q parameter from URL $q=$_GET["q"]; //lookup all links from the xml file if length of q>0 if (strlen($q)>0) { $hint=""; for($i=0; $i<($x->length); $i++) { $d=$x->item($i)->getElementsByTagName('title'); $z=$x->item($i)->getElementsByTagName('url'); if ($d->item(0)->nodeType==1) { //find a link matching the search text if (stristr($d->item(0)->childNodes->item(0)->nodeValue,$q) !=false) { if ($hint=="") { $hint= $z->item(0)->childNodes->item(0)->nodeValue . "<br />" . $d->item(0)->childNodes->item(0)->nodeValue; } else { $hint=$hint . "<br /><br />" . $z->item(0)->childNodes->item(0)->nodeValue . "<br />" . $d->item(0)->childNodes->item(0)->nodeValue; } } } } } // Set output to "no suggestion" if no hint were found // or to the correct values if ($hint=="") { $response="no suggestion"; } else { $response=$hint; } //output the response echo $response; ?>
Can somebody give me an example of how to make it work? From what I understand, stristr will only search one haystack and you can't use arrays, so is there another way? Thanks in advance!
原文:https://stackoverflow.com/questions/13941629
最满意答案
Golang Tour指定导出的名称为
如果名称以大写字母开头,则会导出该名称。 并且在导入包时,您只能引用其导出的名称。 任何“未报告”的名称都不能从包装外部访问。
将
reverse
func的名称更改为Reverse
以使其可导出到主包。 如下所示package stringutil func Reverse(s string) string { r := []rune(s) for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 { r[i], r[j] = r[j], r[i] } return string(r) }
Golang Tour specify exported name as
A name is exported if it begins with a capital letter. And When importing a package, you can refer only to its exported names. Any "unexported" names are not accessible from outside the package.
Change the name of
reverse
func toReverse
to make it exportable to main package. Like belowpackage stringutil func Reverse(s string) string { r := []rune(s) for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 { r[i], r[j] = r[j], r[i] } return string(r) }
相关问答
更多-
你可以使用asNamespace : > methods(cbind) [1] cbind.data.frame cbind.grobGrid cbind.ts* Non-visible functions are asterisked > r <- unclass(lsf.str(envir = asNamespace("stats"), all = T)) > r[grep("cbind.ts", r)] [1] ".cbind.ts" "cbind.ts" stats包中 ...
-
功能内的功能(Functions within functions)[2021-10-03]
这是一个标签声明 。 你可以用goto , break或continue使用它。 来自docs : 定义从未使用过的标签是非法的。 与其他标识符相比,标签不是块作用域,也不与非标签的标识符冲突。 标签的范围是声明它的函数体,并排除任何嵌套函数的主体。 它们在您需要以某种方式更改流量的情况下非常有用,即。 K: for i:=0; i<10; i++ { for j:=0; j<10; j++{ if (somefunction(j)) { continue K ... -
Golang Tour指定导出的名称为 如果名称以大写字母开头,则会导出该名称。 并且在导入包时,您只能引用其导出的名称。 任何“未报告”的名称都不能从包装外部访问。 将reverse func的名称更改为Reverse以使其可导出到主包。 如下所示 package stringutil func Reverse(s string) string { r := []rune(s) for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 ...
-
那里的散列函数通常需要一个ByteArrayAccess实例来输入它们。 只有hashlazy特别需要一个懒惰的ByteString 。 hash (hashlazy x)应该有效,因为Digest有一个ByteArrayAccess实例。 要将多个Digest一起散列,可以使用底层的hashInit / hashUpdates / hashFinalize函数,将所有Digest传递给hashUpdates 。 快速举例: import System.IO import Crypto.Hash impor ...
-
在R上制作玩具包,无法使用R文件中的其他功能(Making a toy package on R, unable to use the other functions in the R file)[2022-06-22]
由于您使用的是Roxygen和devtools,因此您需要执行以下操作: 为要从包中导出的每个函数添加一个#' @export指令 在构建/安装包之前运行document() ,以确保更新NAMESPACE文件。 Since you're using Roxygen and devtools, you need to do the following: Include a #' @export directive for every function you want to export from your ... -
如何定义包中的哪些变量或函数被导出(How to define which variables or functions from a package are exported)[2023-01-13]
load_all函数有一个export_all参数。 来自?load_all 如果为TRUE(默认值),则导出所有对象。 如果为FALSE,则仅导出在NAMESPACE文件中列为导出的对象。 因此,请在您的load_all调用中尝试使用export_all=FALSE 。 The load_all function has an export_all argument. From ?load_all If TRUE (the default), export all objects. If FALSE, e ... -
R不提供隐藏源代码的基础设施[1]。 这样做是违反R开发者的价值观和愿望的。 所以,你不想用R做什么。 [1] http://r.789695.n4.nabble.com/how-to-hide-code-of-any-function-td4474822.html R does not provide infrastructure for hiding source code[1]. Doing that is against the R developers' values and wishes. So ...
-
从另外一些方面来看,它似乎是\docType{import} ,在R 3.1.1中添加,是处理这种情况的最佳可用机制( 如此roxygen2问题中所述 )。 这似乎有效地表现得像我上面的第三个选项,但它的优点是不能明确记录函数参数,因此它们不必保持同步。 看来roxygen2语法就好 #' @export RNifti::xform 生成正确形式的.Rd文件,并满足R CMD check 。 我以前没有使用过这种特殊的语法,这就是问题仍然悬而未决的原因。 我已经确认这适用于未经修改的第三方软件包,因此它看 ...
-
哈希实现都是通过一个通用的hash.Hash 接口提供的 。 如果你查看sha512.New的文档,你会看到它返回一个hash.Hash ,它列出了所有Hash特定的方法。 hash.Hash接口中的第一个字段是嵌入式 io.Writer ,它提供了标准的Write方法。 所有这些值都是文档中的链接,您可以遵循所需的定义,甚至是源代码。 The hash implementations are all provided through a common hash.Hash interface. If you ...
-
编辑:只使用在NAMESPACE导出的函数,并假设它们的Roxygen块中没有@export语句,我们可以对所有export函数进行hacky解析: NAMESPACE <- readLines("NAMESPACE") exported_fns <- grep( "^export\\(", NAMESPACE, value=TRUE ) exported_fn_names <- gsub( "export\\((.*)\\)", "\\1", exported_fns ) fn_match_re <- p ...