PHP未定义的偏移量,涉及file_put_contents和explode()

时间:2018-01-24 09:22:58

标签: php

我得到了未定义的偏移量。请帮忙,我找不到问题所在:(

var addjobfixedstart = document.getElementById("jobfixedstart_cb").checked

结果是.txt文件:

5000013,0.00,0.00,50.00,0 + 5006529,0.00,0.00,50.00,0

我想要的是.txt:

5000013,0.00,0.00,50.00,0

5006529,0.00,0.00,50.00,0

2 个答案:

答案 0 :(得分:0)

您可以简单地使用 public static IEnumerable<Client> GetClients() { return new List<Client> { new Client { ClientId = "client", // no interactive user, use the clientid/secret for authentication AllowedGrantTypes = GrantTypes.ClientCredentials, // secret for authentication ClientSecrets = { new Secret("secret".Sha256()) }, // scopes that client has access to AllowedScopes = { "api1" }, AccessTokenLifetime=3600 } 函数来替换preg_replace空格

+

答案 1 :(得分:0)

我认为问题是由于循环中的<= - 因为计数器从零开始,它将超过实际计数1,从而产生undefined index错误。

<?php

    $title = $_GET['title'];
    $data = $_GET['data'];

    //title is testing
    //data is 5000013,0.00,0.00,50.00,0+5006529,0.00,0.00,50.00,0

    $lines = explode( "+", $data );
    $file = $title . ".txt";


    for( $i=0 ; $i < count( $lines ); $i++ ){
        $content = $lines[ $i ] . PHP_EOL;

        $status = file_put_contents( $file, $content, FILE_APPEND );

        echo $status ? 'SUCCESS: data written to txt file' : 'FAIL: could not write to txt file';
    }
?>

修改 在源数据中使用+是不明确的,因为+在编码时用于网址(空格)

<?php

    $title = $_GET['title'];
    $data = urlencode( $_GET['data'] );

    $lines = explode( "+", $data );
    $file = __DIR__ . '/' . $title . ".txt";


    for( $i=0 ; $i < count( $lines ); $i++ ){
        $content = urldecode( $lines[ $i ] ) . PHP_EOL;

        $status = file_put_contents( $file, $content, FILE_APPEND );

        echo $status ? 'SUCCESS: data written to txt file' : 'FAIL: could not write to txt file';
    }
?>