开发者

Delete first character of string in-place using PHP

开发者 https://www.devze.com 2023-03-09 12:37 出处:网络
There is this post: PHP Subtract First Character of String It advices me to use substr(...); I want to keep a rolling text to log if an error occurs, (The 1000 latest characters from a stream) but

There is this post:

PHP Subtract First Character of String

It advices me to use substr(...);

I want to keep a rolling text to log if an error occurs, (The 1000 latest characters from a stream) but it seems like there would be a better开发者_如何学编程 way than to create a 1000 character string from a 1001 character string, then assigning that string to the latter.

I will be doing this in a very tight loop, so performance should not be negligible (even though I haven't measure this yet).

Is there any way to delete first character of a string in-place?


This should work properly but not a good choice

<?php
    $str = '12345678';
    $str[0] = null;
    echo $str; // output: 2345678
?>

Since

echo strlen($str); // output: 8 because first character is not deleted, it is "hidden"

Take me over 500 points if this is helpful (:


The obvious question would be why you would want to do this in PHP? You probably have operating system support for rolling logs.

However, if you wish to have a robust solution you are most likely best off using substr.

Another option could be to use the array access for a string:

unset(your_string[0]);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号