2015-12-30 146 views
0

我從XML中過濾了整個標題。從xml在php中搜索過濾器

if(isset($_POST["submit"])) 
     { 

     $state=$_POST["state"]; 

     $title=$_POST["title"]; 


     $result = simplexml_load_file('sample.xml'); 
     $record = $result-> xpath('(/jobs/job[positiontitle="'.$title.'"])|(/jobs/job[state="'.$state.'"])'); 

     foreach ($record as $row): 
$jobid=$row->jobid; 

?> 
     <li class="result"> 
     <a href="<?php print base_path(); ?>job/?<?php print $jobid;?>"> 
     <h3 class="title"><?php $title=$row->positiontitle; 
     echo $title; ?></h3> 
     </li> 
<?php 

     endforeach; 
?> 

這裏我的XML

<jobs> 
<job> 
<jobid> 
<![CDATA[ 02304485 ]]> 
</jobid> 
<positiontitle> 
<![CDATA[ Maintenance Manager ]]> 
</positiontitle> 
</job> 
</jobs> 

如果鍵入的標題是「維護」,標題應該是filtered.In SQL我們可以用同樣的操作,進行篩選。這裏應該怎麼做?

回答

0

我認爲這將有助於如果您在xpath表達式中使用normalize-space來去除前導空白和尾隨空白。

你也許該行更改:

$record = $result-> xpath('(/jobs/job[positiontitle="'.$title.'"])|(/jobs/job[state="'.$state.'"])'); 

這一行:

$record = $result->xpath('/jobs/job[normalize-space(positiontitle)="' . $title . '"]|/jobs/job[normalize-space(jobid)="' . $state . '"]');