【问题标题】:Trying to replace part of a file name with str_replace试图用 str_replace 替换文件名的一部分
【发布时间】:2013-07-04 02:42:42
【问题描述】:

我正在为天气雷达和警告图像创建一个 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"
}

【问题讨论】:

  • 是我弄错了,还是get_text()file_get_contents() 做同样的事情?
  • 显示var_dump($matches[2])
  • 这看起来很痛苦。为什么这个"/(a href\=\")([^\?\"]*)(\")/i" 这个'~(a href=")([^\?"]*)(")~i' 或者更好的说这个'~a href="([^\?"]*)"~i' 因为第一次和第三次捕获毫无价值?
  • 我还没有完全理解这一切。我是自学成才并以此作为参考stackoverflow.com/questions/8169293/…你能进一步解释一下吗?

标签: php str-replace


【解决方案1】:

我认为 str_replace 不起作用,因为您需要替换的字符串不是“NOR”而是“N0R”(N-zero-R)。

【讨论】:

  • 谢谢!看了太久了。我讨厌错过这样愚蠢的事情。
  • 不客气。有时你所需要的只是一双新鲜的眼睛。感谢您给我第一个接受的答案:-)
猜你喜欢
  • 2011-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-23
  • 2016-10-07
  • 2016-11-02
  • 2013-12-03
相关资源
最近更新 更多