Str_replace不起作用

时间:2015-11-11 11:09:10

标签: php

我想编写一些导入CSV文件的代码,获取有用位,然后将其放入数据库中。现在我已经写了代码,只是将它输出到一个表中,这样我就可以检查我的所有值是否正常工作。到现在为止还挺好!但是我对我的CSV文件中的某个字段的显示方式不满意。我想编辑它。目前该领域似乎是这样的:

模式:5;速度:mSpeed速度;倾斜范围:完整

我想剥离除模式以外的所有内容。要做到这一点,我正在使用

list($mode) = explode(";", $fixture_options);

输出:

模式:5

到目前为止很好!下一步是摆脱'模式:'我希望简单地使用:

$newmode = str_replace("Mode:","", $mode);

但这似乎不起作用?这是我的完整代码。毫无疑问,这很简单!

  <?php

    function Table($name,$value)
        {
            echo "<tr><td>$name</td><td>$value</td></tr>";
        }



ini_set('auto_detect_line_endings', TRUE);
$rows = array_map('str_getcsv', file('test.csv'));
$header = array_shift($rows); // Strip out the colum headers from the CSV
$csv = array();
foreach ($rows as $row) {


    /*** Remove all the unwanted Colums **/

            unset($row[2]); // Remove Dimmer
            unset($row[6]); // Remove Lens
            unset($row[7]); // Remove Hookup
            unset($row[8]); // Remove Purpose
            unset($row[9]); // Remove Colour
            unset($row[10]); // Remove Gobo
            unset($row[11]); // Remove Focus
            unset($row[16]); // Remove Lamp Type
            unset($row[17]); // Remove Offset
            unset($row[18]); // Remove X
            unset($row[19]); // Remove Y
            unset($row[20]); // Remove Z
            unset($row[21]); // Remove Pan
            unset($row[22]); // Remove Tilt
            unset($row[23]); // Remove Spin
            unset($row[25]); // Remove Notes
            unset($row[26]); // Remove Footnotes
            unset($row[28]); // Remove # of colour frames
            unset($row[29]); // Remove # of lamps
            unset($row[30]); // Remove Circuit Type
            unset($row[31]); // Remove Model        
            unset($row[32]); // Remove Cost
            unset($row[34]); // Remove Console
            unset($row[35]); // Remove Layer
            unset($row[36]); // Remove Tag

        $channel =          $row[0];
        $patch =            $row[1];
        list($uni, $address) = explode(".", $patch); // we convert the address into univers and address.
        $spot =             $row[3];
        $position =         $row[4];
        $type =             $row[5];
        $circuit_name =     $row[12];
        $circuit_number =   $row[13];
        $fixture_options =  $row[14];

        list($mode) = explode(";", $fixture_options); // We strip the other mode options out
        $newmode = str_replace("Mode:","", $mode); 

        $wattage =          $row[15];
        $weight =           $row[24];
        $status =           $row[33];



echo "<table>";
Table('Channel',$channel);
Table('Universe',$uni);
Table('Address',$address);
Table('Spot',$spot);
Table('Position',$position);
Table('Type',$type);
Table('Circuit Name',$circuit_name);
Table('Circuit Number',$circuit_number);
Table('Fixture Mode',$newmode);
Table('Wattage',$wattage);
Table('Weight',$weight);
Table('Status',$status);

echo "</table>";


}

1 个答案:

答案 0 :(得分:3)

我的建议:一次性使用正则表达式:

$string = "Mode: 5; Speed: mSpeed Speed; Tilt Range: Full";

preg_match('/Mode: (\d+);/',$string, $matches);

//result in $matches[1];