微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 计算开始的空格

我想计算(在单个正则表达式中)字符串开头的所有空格.

我的想法:

$identSize = preg_match_all("/^( )[^ ]/",$line,$matches);

例如:

$example1 = " Foo"; // should return 1
$example2 = "  Bar"; // should return 2
$example3 = "   Foo bar"; // should return 3,not 4!

任何提示,我如何解决它?

$identSize = strlen($line)-strlen(ltrim($line));

或者,如果你想要正则表达式,

preg_match('/^(\s+)/',$matches);
$identSize = strlen($matches[1]);

原文地址:https://www.jb51.cc/php/138996.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐