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]);

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...