1

我正在尝试将 txt 文件存储在 php 变量中,我已经拥有该功能,但有一个问题我无法真正解决。

我想使用变量作为此类文件的名称来读取文件,当我这样做时,脚本不会读取文件,但是当我明确编码名称时,它可以正常工作。

我有以下代码:

$mga_file[] = "./MGA/mga_" . preg_replace("[^a-zA-Z]", "", trim(strtolower($ar_mga[0]))) . "_" . preg_replace("[^a-zA-Z]", "", trim(strtolower($db_producto))) . ".txt";
$mga_file[] = "./MGA/mga_" . preg_replace("[^a-zA-Z]", "", trim(strtolower($ar_mga[0]))) . "_led.txt";
$mga_file[] = "./MGA/mga_high_led.txt";
$mga_file[] = "./MGA/mga_high_".strtolower($db_producto).".txt";

我编辑了代码,认为我的变量$ar_mga[0]可能有其他字符,但它仍然不起作用。使用这个数组和下面的代码,我有不同的结果:

foreach($mga_file as $file){
  $homepage = file_get_contents($file, FILE_USE_INCLUDE_PATH);
  echo "[" . $file . "] : ";
  echo "<pre>"; print_r($homepage); echo "</pre>";
}     

给我以下结果:

Array [$mga_file]
(
    [0] => ./MGA/mga_high_led.txt //Not Working
    [1] => ./MGA/mga_high_led.txt //Not Working
    [2] => ./MGA/mga_high_led.txt //Working
    [3] => ./MGA/mga_high_led.txt //Working
)

如您所见,所有 4 个值的文件名完全相同,如果我只读取最后 2 个值的文件会发生什么情况?

编辑:似乎我忽略了一个巨大的细节,该$ar_mga[0]变量包含我试图使用的值,但它被包含在一个跨度标记中(在我检查页面 html 之前我从未见过),因此从未工作过。从变量中剥离标签使脚本正常工作。

我的错!

4

1 回答 1

0

Seems I overlooked one huge detail, the $ar_mga[0] variable contained the value I was trying to use, but it was enclosed on a span tag (which I never saw until I examined the page html), and never worked because of this. Striping the tag from the variable made the script work correctly.

My bad!

于 2013-05-22T00:26:14.923 回答