亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

strstr 不適用于 xml 文件中的變量

strstr 不適用于 xml 文件中的變量

PHP
慕田峪9158850 2022-01-02 16:21:11
我對 php 中的函數 strstr 有一些問題。 $text=file_get_contents("text.txt");  echo $text.'<br><br>'; $xml = simplexml_load_file('listmv.xml'); foreach($xml->item as $item) {$quoi="sangs rgyas";if (strstr($text,$quoi)) { echo 'yes';} }strstr 返回“是”, $text=file_get_contents("text.txt"); echo $text.'<br><br>'; $xml = simplexml_load_file('listmv.xml'); foreach($xml->item as $item) {$quoi=$item->tib;if (strstr($text,$quoi)) { echo 'yes';} }不起作用。這是怎么回事?XML 文件:-<list>   <item>    <tib>sangs rgyas</tib>    <ref>1524</ref>  </item>   <item>    <tib>rgya gar skad du</tib>    <ref>1522</ref>  </item>   <item>    <tib>shes pa dang</tib>    <ref>1523</ref>  </item>   <item>    <tib>'tsho ba dang</tib>    <ref>1525</ref>  </item> </list> 
查看完整描述

2 回答

?
猛跑小豬

TA貢獻1858條經驗 獲得超8個贊

當您在循環中從 SimpleXML 返回一個項目時...


foreach($xml->item as $item)

{

    $quoi=$item->tib;

    if (strstr($text,$quoi)) { echo 'yes';}

}

如果你添加


var_dump($quoi);

你會看到它實際上是一個 SimpleXMLElement 而不是一個字符串......


class SimpleXMLElement#5 (1) {

  public ${0} =>

  string(11) "sangs rgyas"

}

之類的東西echo會將其轉換為字符串,因此請使用


if (strstr($text,(string)$quoi)) { echo 'yes';}


查看完整回答
反對 回復 2022-01-02
?
子衿沉夜

TA貢獻1828條經驗 獲得超3個贊

它正在返回一個您需要將其轉換為字符串的對象。更正的代碼


 $text=file_get_contents("text.txt");

 echo $text.'<br><br>';

 $xml = simplexml_load_file('listmv.xml');

 foreach($xml->item as $item)

 {

$quoi=$item->tib;

if (strstr($text,$quoi->__toString())) { echo 'yes';}

 }


查看完整回答
反對 回復 2022-01-02
  • 2 回答
  • 0 關注
  • 168 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號