English 中文(简体)
使用PHP删除字符串的第一个字符
原标题:Delete first character of string in-place using PHP
  • 时间:2011-05-30 13:20:42
  •  标签:
  • php
  • string

有这样的帖子:

PHP减去字符串的第一个字符

它建议我使用substr(…)

如果发生错误,我想保留一个滚动文本来记录(流中最新的1000个字符),但似乎有一种更好的方法,而不是从1001个字符的字符串中创建1000个字符,然后将该字符串分配给后者。

我将在一个非常紧密的循环中完成这项工作,所以性能不应该被忽略不计(尽管我还没有衡量这一点)。

有没有办法删除字符串的第一个字符?

问题回答

这应该可以正常工作,但不是一个好的选择

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

自从

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

如果这有帮助的话,给我500多分(:

显而易见的问题是,为什么要在PHP中这样做?您可能有操作系统对滚动日志的支持。

但是,如果您希望有一个健壮的解决方案,那么最好使用substr

另一种选择是对字符串使用数组访问:

unset(your_string[0]);




相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签