开发者

PHP ignore 5th character?

I have a simple php question.

in my php, I have this:

$variable = 'howareyou';

is it possi开发者_JS百科ble to somehow modify the code so it only counts up to 6th character of the variable?

so after, when echo'd

it would say howare instead of howareyou.

I need to filter it with a number, like 5th or 6th. Is this possible?

Thanks!


You can use substr() to retrieve part of a string:

$substring = substr($variable, 0, 6);


Just use substring.

$variable = substr($variable, 0, 6)

The syntax is substr(string, start, length) and remember that these are zero indexed.


try the following code:

$var=substr($variable,0,6);


You can use

echo substr($string, 0, 6);                            


Just a note. If you use UTF8 (for example) and another non-english language or fancy UTF characters you will need the mb_(...) functions. So substr($string, 0, 6) becomes mb_substr($string, 0, 6)

Otherwise you risk splitting a multibyte characted in half, and it isn't pretty. This also means that if there are multibyte characters the regular substr will count them as 2 resulting in much shorter string that you'd expect.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜