在PHP中使用substr()无法处理xpath结果

在PHP中使用substr()无法处理xpath结果,php,xpath,substr,Php,Xpath,Substr,我仍然在学习xpath和PHP。我试图剪切xpath使用substr返回的字符串,但我没有成功。我创建了一个单独的字符串来测试我是否错误地使用了substr。我通过测试字符串而不是xpath结果字符串实现了我想要的结果 $temp = $itemPrices->item(0)->nodeValue; // value = "$2.69 per bottle" //$temp = "$2.69 per bottle"; // testing string //var_dump($te

我仍然在学习xpath和PHP。我试图剪切xpath使用substr返回的字符串,但我没有成功。我创建了一个单独的字符串来测试我是否错误地使用了substr。我通过测试字符串而不是xpath结果字符串实现了我想要的结果

$temp = $itemPrices->item(0)->nodeValue; // value = "$2.69 per bottle"
//$temp = "$2.69 per bottle"; // testing string

//var_dump($temp); // says both $temp's are strings (tested individually)

$itemPrice = substr($temp,1,4); // hoping for "2.69" (only works with second $temp)
echo $itemPrice; // nothing when using first $temp (xpath result)

如果您对此有任何帮助,我们将不胜感激。

我忽略了每个xpath结果附带的周围空格。我的substr语句将字符1到4切掉,因为这些字符是空格,所以它返回这些空格

通过改变 $temp=$itemPrices->item0->nodeValue

到 $temp=trim$itemPrices->item0->nodeValue;周围的空格被删除,这使得substr语句可以按我想要的方式运行


谢谢大家的帮助

使用$temp=$项目价格->项目0->节点价值;更改观察值/结果?您一定在某个地方输入了错误,因为->nodeValue是一个字符串属性,所以对它执行substr应该可以。否则,请添加足够的代码以使其可复制。此外,substrstrstok$temp',1可能更好,以防您的价格为每瓶21.95美元。仍然没有任何回应。我应该尝试使用preg_match和合适的正则表达式吗?但是不太确定如何执行正则表达式..var_dump显示前面字符串的长度。两种情况下的长度相同吗?