我正在为天气雷达和警告图像创建一个JQuery循环。我的雷达覆盖功能很好。包含警告文件的目录包含每个文件的2个版本,但其中一个具有与雷达图像类似的文件名。所以我决定使用str_replace将文件名的一部分从雷达图像替换为警告图像。由于某种原因,str_replace无法正常工作,我不能为我的生活找出原因。
提前感谢您的帮助。
// Used to get radar images
echo '<div class="wp-forecast-img-cur">'."\n";
$matches = array();
preg_match_all("/(a href\=\")([^\?\"]*)(\")/i", get_text('http://radar.weather.gov/ridge/RadarImg/N0R/BOX/'), $matches);
foreach($matches[2] as $k => $match) {
if ($k < 1) continue;
echo '<img src="http://radar.weather.gov/ridge/RadarImg/N0R/BOX/' . $match . '" />'."\n";
}
echo '</div>'."\n";
// Used to get warning images
echo '<div class="wp-forecast-img-warn">'."\n";
$matches = array();
preg_match_all("/(a href\=\")([^\?\"]*)(\")/i", get_text('http://radar.weather.gov/ridge/RadarImg/N0R/BOX/'), $matches);
foreach($matches[2] as $k => $match) {
if ($k < 1) continue;
echo '<img src="http://radar.weather.gov/ridge/Warnings/Short/BOX/' . str_replace("NOR", "Warnings", $match) . '" />'."\n";
}
echo '</div>'."\n";
这一行只是回显原始文件名,而不是用警告替换NOR。
echo '<img src="http://radar.weather.gov/ridge/Warnings/Short/BOX/' . str_replace("NOR", "Warnings", $match) . '" />'."\n";
如果需要,这是get_text函数。
function get_text($filename)
{
$fp_load = fopen("$filename", "rb");
if ( $fp_load )
{
while ( !feof($fp_load) )
{
$content .= fgets($fp_load, 8192);
}
fclose($fp_load);
return $content;
}
}
变量转储
array(32) {
[0]=> string(20) "/ridge/RadarImg/N0R/"
[1]=> string(25) "BOX_20130703_2220_N0R.gif"
[2]=> string(25) "BOX_20130703_2232_N0R.gif"
[3]=> string(25) "BOX_20130703_2238_N0R.gif"
[4]=> string(25) "BOX_20130703_2243_N0R.gif"
[5]=> string(25) "BOX_20130703_2249_N0R.gif"
[6]=> string(25) "BOX_20130703_2301_N0R.gif"
[7]=> string(25) "BOX_20130703_2306_N0R.gif"
[8]=> string(25) "BOX_20130703_2318_N0R.gif"
[9]=> string(25) "BOX_20130703_2324_N0R.gif"
[10]=> string(25) "BOX_20130703_2330_N0R.gif"
[11]=> string(25) "BOX_20130703_2335_N0R.gif"
[12]=> string(25) "BOX_20130703_2347_N0R.gif"
[13]=> string(25) "BOX_20130703_2353_N0R.gif"
[14]=> string(25) "BOX_20130703_2359_N0R.gif"
[15]=> string(25) "BOX_20130704_0004_N0R.gif"
[16]=> string(25) "BOX_20130704_0016_N0R.gif"
[17]=> string(25) "BOX_20130704_0022_N0R.gif"
[18]=> string(25) "BOX_20130704_0027_N0R.gif"
[19]=> string(25) "BOX_20130704_0033_N0R.gif"
[20]=> string(25) "BOX_20130704_0045_N0R.gif"
[21]=> string(25) "BOX_20130704_0056_N0R.gif"
[22]=> string(25) "BOX_20130704_0108_N0R.gif"
[23]=> string(25) "BOX_20130704_0114_N0R.gif"
[24]=> string(25) "BOX_20130704_0125_N0R.gif"
[25]=> string(25) "BOX_20130704_0137_N0R.gif"
[26]=> string(25) "BOX_20130704_0148_N0R.gif"
[27]=> string(25) "BOX_20130704_0154_N0R.gif"
[28]=> string(25) "BOX_20130704_0206_N0R.gif"
[29]=> string(25) "BOX_20130704_0211_N0R.gif"
[30]=> string(25) "BOX_20130704_0223_N0R.gif"
[31]=> string(25) "BOX_20130704_0229_N0R.gif"
}
答案 0 :(得分:2)
我认为str_replace不起作用,因为你需要替换的字符串不是“NOR”而是“N0R”(N-zero-R)。