簡體   English   中英

Str_replace不起作用

[英]Str_replace doesn't work

我想編寫一些導入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>";


}

我的建議:一次性使用正則表達式:

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

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

//result in $matches[1];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM