I was wondering if you could help. The code below pulls in XML data from an external XML file and then re-formats it into table data. All works perfect. However, some of the data from the XML file needs to be changed and so is it possible to use str_replace or preg_replace for each individual node below? I do not know how to write the str_replace for each node. I thought perhaps something like this "{$hour->weatherCod} . str_replace (10, Sun);" would work but it doesn't. Would smeone be able to tell me the correct format? I need something easy as I am more of an actionscript guy rather than php.
我想知道你是否可以提供帮助。下面的代码从外部XML文件中提取XML数据,然后将其重新格式化为表数据。一切都很完美。但是,需要更改XML文件中的某些数据,因此可以为下面的每个单独节点使用str_replace或preg_replace吗?我不知道如何为每个节点编写str_replace。我想也许是这样的“{$ hour-> weatherCod} .str_replace(10,Sun);”会工作,但事实并非如此。请问我能告诉我正确的格式吗?我需要一些简单的东西,因为我更像是一个动作脚本而不是php。
Many thanks if youre able to help!
非常感谢你能提供帮助!
<?php
// load SimpleXML
$data = new SimpleXMLElement('xml_file.xml', null, true);
foreach($data->weather as $weather)
{
foreach ($weather->hourly as $hour)
{
echo <<<EOF
<tr>
<td>{$weather->day}</td>
<td>{$weather->Ctemp1}°c</td>
<td>{$weather->Ctemp2}°c</td>
<td>{$hour->time}</td>
<td>{$hour->tempC}°c</td>
<td>{$hour->tempF}°f</td>
<td>{$hour->windMiles}mph</td>
<td>{$hour->windKmph}km/h</td>
<td>{$hour->windDegree}°</td>
<td>{$hour->winddir}</td>
<td>{$hour->weatherCod}</td>
<td>{$hour->weatherIconUrl}</td>
<td>{$hour->precipMM}mm</td>
<td>{$hour->humidity}%</td>
<td>{$hour->visibility}</td>
<td>{$hour->pressure}mb</td>
<td>{$hour->cloudcover}</td>
<td>{$hour->sigHeight_m}m</td>
<td>{$hour->swellHeight_m}m</td>
<td>{$hour->swellDir}°</td>
<td>{$hour->swellPeriod_secs}s</td>
<td>{$hour->waterTemp_C}°c</td>
<td>{$hour->waterTemp_F}°f</td>
</tr>
EOF;
}
}
echo '</table>';
?>
1 个解决方案
#1
0
Actually what you try to do with {$hour->weatherCod} . str_replace (10, Sun); ?
实际上你尝试用{$ hour-> weatherCod}做什么。 str_replace(10,Sun); ?
Look how it works, it takes 3 parameters not 2. http://php.net/manual/en/function.str-replace.php
看它是如何工作的,它需要3个参数而不是2. http://php.net/manual/en/function.str-replace.php
So may by something like this
可能是这样的事情
str_replace (10, Sun, $hour->weatherCod);
#1
0
Actually what you try to do with {$hour->weatherCod} . str_replace (10, Sun); ?
实际上你尝试用{$ hour-> weatherCod}做什么。 str_replace(10,Sun); ?
Look how it works, it takes 3 parameters not 2. http://php.net/manual/en/function.str-replace.php
看它是如何工作的,它需要3个参数而不是2. http://php.net/manual/en/function.str-replace.php
So may by something like this
可能是这样的事情
str_replace (10, Sun, $hour->weatherCod);