Php 将变量分配给xml中的值
我的XML文件是:Php 将变量分配给xml中的值,php,xml,parsing,Php,Xml,Parsing,我的XML文件是: <hotels xmlns=""> <hotels> <hotel> <noofrooms>10</noofrooms> <website></website> <imageref>oias-sunset-2.jpg|villas-agios-nikolaos-1.jpg|villas-agios-nikolaos-24.jpg|vi
<hotels xmlns="">
<hotels>
<hotel>
<noofrooms>10</noofrooms>
<website></website>
<imageref>oias-sunset-2.jpg|villas-agios-nikolaos-1.jpg|villas-agios-nikolaos-24.jpg|villas-agios-nikolaos-41.jpg</imageref>
<descr>blah blah blah</descr>
<hotelid>119</hotelid>
</hotel>
</hotels>
如果您的xml有效,您可以使用(如注释中所建议的): 要查看您现在拥有的内容,您可以使用:
print_r($images);
请参见使用SimpleXML解析文件,然后使用explode函数将其拆分为变量
$doc = new SimpleXMLElement($yoursqlstring);
foreach($doc->hotel as $hotel){
$refs = explode('|',$hotel->imageref);
}
试试这个:
$images[] = '';
$xmlString = 'your xml';
$xml = new SimpleXMLElement($xmlString);
foreach ($xml->hotels->hotel->imageref as $img_urls) {
$images[] = $img_urls;
}
现在,您可以通过
echo $image[1];
echo $image[2];
.
.
.
加载整个xml文件(例如,simplexml\u load\u file
),然后在imageref元素上使用explode(“|”,$element)
,搜索是您的朋友:)
$images[] = '';
$xmlString = 'your xml';
$xml = new SimpleXMLElement($xmlString);
foreach ($xml->hotels->hotel->imageref as $img_urls) {
$images[] = $img_urls;
}
$doc = new SimpleXMLElement($yoursqlstring);
foreach($doc->hotel as $hotel){
$image_array = explode('|', $hotel->imageref);
foreach($image_array as $k => $img)
$image[$k+1] = $img;
}
echo $image[1];
echo $image[2];
.
.
.