2013-12-19 42 views
2

我试图读取该文件名组成的文件,并写入数据库查询中使用的foreach - MV class.module.example.php class.module.examplejason.php为每名进入一份文件。的输出额外的点,而在PHP

这是我的代码:

$lines = file('/home/Staging/txts/textFile.txt'); 
$developer = "jason"; 
foreach ($lines as $line_num => $line) 
{ 
    $fileNameArray = array_reverse(explode("/",$line)); 
    $fileNameArray2 = array_reverse(explode(".",$fileNameArray[0])); 
    $fileNameArray2[1] = $fileNameArray2[1].$developer; 
    $fileNameArray3 = array_reverse($fileNameArray2); 
    $j=0; 

    for($i=1;$i<count($fileNameArray2);$i++) 
    { 
      $dot="."; 
      if($j==$i) 
      { 
       $dot=""; 
      } 
      $devFileName = $devFileName.$fileNameArray3[$i].$dot; 
      $j++; 
    } 
    $fileNameArray4 = $fileNameArray; 
    $fileNameArray4[0] = $devFileName; 

    $fileNameArray4 = implode("/",array_reverse($fileNameArray4)); 

    echo "mv ".$fileNameArray[0]." ".$fileNameArray4."\r\n"; 
} 

问题:

  1. 我在for循环结束时得到的输出是:class.module.examplejason.php . 为什么我得到最后.?我究竟做错了什么?

    如果我的文件中有2名,像/home/Staging/class.module.example.php和/home/Staging/class.module.anotherExample.php,输出为:

    mv class.module.example.php /home/Staging/class.module.examplejason.php . mv class.module.anotherExample.php /home/Staging/class.module.anotherExamplejason.php .

回答

0

您的for循环不正确。您可以使用implode用于相同的目的:

<?php 
$lines = file('/home/Staging/txts/textFile.txt'); 
$developer = "jason"; 
foreach ($lines as $line_num => $line) 
{ 
    $fileNameArray = array_reverse(explode("/",$line)); 
    $fileNameArray2 = array_reverse(explode(".",$fileNameArray[0])); 
    $fileNameArray2[1] = $fileNameArray2[1].$developer; 
    $fileNameArray3 = array_reverse($fileNameArray2); 

    $fileNameArray4 = $fileNameArray; 
    $fileNameArray4[0] = implode(".",$fileNameArray3); 

    $fileNameArray4 = implode("/",array_reverse($fileNameArray4)); 

    echo "mv ".$fileNameArray[0]." ".$fileNameArray4."\r\n"; 
} 
+0

谢谢。它起到了魅力! – mercy

0

尝试改变

$j=0; 

$j=count($fileNameArray2); 
+0

我不认为这会工作..然后,我将不得不改变整个循环。 – mercy

+0

@mercy无论如何,循环是错误的。这将是朝着正确方向迈出的一步。您还需要删除'$ j ++'并将'$ i'初始化为'0'。 – Zenexer

0

这是因为你是echo荷兰国际集团$fileNameArray4,您在上面使用$fileNameArray4 = $fileNameArray;设置。 $fileNameArray是一个数组(由array_reverse(explode("/",$line))生成),所以你从中得到一个点。不要尝试回显数组。

+0

数组不被回显。它会先爆裂,然后变成一串。由于变量名称,这是误导。 – Zenexer

+0

我现在试过'print_r($ fileNameArray4)'。我得到了'/home/Staging/class.module.anotherExamplejason.php .'这是一个字符串对吗? – mercy