开发者

Backslash + number interpreted by PHP as an octal value, how to block this?

I have to reformat a string that can contain the following :

$string = "---8\7----";

The problem is that '\7' is interpreted by PHP as an octal value instead of just '\' and then '7'.

How can I block php from doing this please ? Of course, I need 开发者_如何学运维to keep the '\' Thanks.


As others have said, escape the backslash with another backslash, but I feel I should mention why this happen. Your string is written with double quote marks, which in PHP enters into template mode where certain rules gets invoked, and one of them are indeed what you've stumbled upon. Using single quotes fixes this also, by it not doing any templating ;

$string = '---8\7----';

It's the pro's and con's of the two modes, really, but as long as you're aware of this, it shouldn't bite you too hard in the future. :)


You need to give Backslash for escaping that charatcer.

$string = "---8\\7----";


Escape the backslash.

$string = "---8\\7----";


Use two slashes:

$string = "---8\\7----";


Use escape char:

$string = "---8\\7----";
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜