substr_count not working with new lines?

[亡魂溺海] 提交于 2019-12-11 07:55:19

问题


This is driving me nuts, it keeps returning 0

substr_count('df
d
fd
f
df', '\n');

if I use a letter like "d", it works fine

substr_count('df
d
fd
f
df', 'd');

Can anyone shed some light on this?

Thanks


回答1:


You need to use double quotes for control characters:

var_dump(substr_count('df
d
fd
f
df', "\n"));



回答2:


'\n' is not the same as "\n". '\n' is text comprising a slash and the letter "n", whereas "\n" is a newline character.

Suggest you read the relevant section of the PHP manual about strings, particularly where it talks about single and double quoted strings.




回答3:


In addition to Alix and Mark: please use PHP_EOL instead of \n. Newlines differ on the different platforms (Windows/Linux/Mac), but PHP_EOL is always right. See this question for more info on the subject: When do I use the PHP constant "PHP_EOL"?



来源:https://stackoverflow.com/questions/10367971/substr-count-not-working-with-new-lines

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!