-2

我需要在所有节点上的 XML 文件中创建字符串搜索。

---目录.xml---

<?xml version="1.0" encoding="ISO-8859-1"?>
<Catalog>
<Category>
<Name>Biscuit</Name>
<Location>
   <Id>Butter</Id>
   <Description>The butter biscuit cost $10 per pack</Description>
  </Location>
  <Location>
   <Id>Chocolate</Id>
   <Description>The chocolate biscuit cost $20 per pack</Description>
  </Location>
 </Category>
 <Category>
  <Name>Cake</Name>
  <Location>
   <Id>Cup</Id>
   <Description>This is a cup cake</Description>
  </Location>
  <Location>
   <Id>Slice</Id>
   <Description>This is a slice cake</Description>
  </Location>
 </Category>
</Catalog>

---search.php---

<?php
$catalog = simplexml_load_file("catalog.xml");
$category = $catalog->Category;
$location = $category->Location;

foreach($location->Description as $desc)
{
 $string = string($desc);
 $find = 'chocolate';

 $result = strpos($string, $find)

 if ($result !== false)
 {
  echo $result;
 }
 else
 {
  echo "No Result";
 }
}
?>

我收到的错误是:

Parse error: syntax error, unexpected T_IF on line 14

由于节点和节点都有“巧克力”,我需要显示两个节点结果。

----新修订守则 / 2012 年 11 月 20 日---

<?php

$catalog = simplexml_load_file("catalog.xml");

$find = "chocolate";

$lcFind = strtolower($find);
$ll = implode('', range('a', 'z'));
$ul = strtoupper($ll);
$xpath_result = $catalog->xpath("//*[contains(translate(text(), '$ul','$ll'),'$lcFind')]");

if ($xpath_result) {
foreach ($xpath_result as $res)  {

$category = $catalog->Category;
$name = $category->Name;
$loc = $category->Location;
$id = $loc->Id;

echo "Category: ", $name, "<br />";
echo "ID: ", $id, "<br />";
echo "Description :", $res, "<br />";
}
}
else {
echo "No matching descriptions found for word '<i>$find</i>'<br />";
}
?>

结果(错误):

类别:饼干 ID:黄油 //这应该是“巧克力” 描述:巧克力 //这应该是“巧克力”的描述 类别:饼干 ID:黄油 描述:巧克力饼干每包 20 美元

4

1 回答 1

1

它应该是...

$result = strpos($string, $find);

你应该在 PHP 中用分号结束你的语句。使用带有 PHP 语法检查的 IDE(甚至是文本编辑器)可能是一个不错的主意。

...然而问题只是开始(我不是在谈论string这里不存在的功能)。看,使用此代码,您试图仅在第一个类别的第一个位置的描述中搜索某些术语。如果这实际上是任务,好吧,但不知何故,我觉得你的初衷用这个更好地表达:

$found = false;
foreach ($catalog->Category as $category) {
  foreach($category->Location as $location) {
    $description = "{$location->Description}";
    $result = strpos($description, $find);
    if ($result !== FALSE) {
      echo "Word '<i>$find</i>' found in <b>$description</b> at position " . ($result + 1) . '.<br />';
      $found = true;
    }
  }
}
if (! $found) {
  echo "No matching descriptions found for word '<i>$find</i>'<br />";
}

甚至可以使用 XPath 优化这一点 - 特别是如果您实际上不需要回显该位置:

$xpath_result = $catalog->xpath("//Description[contains(text(),'$find')]");
if ($xpath_result) {
  foreach ($xpath_result as $res)  {
    echo "Word '<i>$find</i>' found in <b>$res</b><br />";
  }
}
else {
  echo "No matching descriptions found for word '<i>$find</i>'<br />";
}

...或者,对于不区分大小写的搜索:

$lcFind = strtolower($find);
$ll = implode('', range('a', 'z'));
$ul = strtoupper($ll);
$xpath_result = $catalog->xpath("//*[contains(translate(text(), '$ul', '$ll'),'$lcFind')]");
... // the rest of code is the same
于 2012-11-19T08:38:07.890 回答