-2

すべてのノードの XML ファイルで文字列の検索を作成する必要があります。

---catalog.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: バター 説明 :チョコレート ビスケットの価格は 1 パックあたり 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 に答える