0

我能想到的最好的是:

strlen(preg_replace('/^([\\*]*)\s(.+)/',"$1",$line));
^^这似乎给出了字符串的长度。^^

编辑:我认为我应该澄清我要查找的字符是'*'

4

2 回答 2

3

preg_match 允许使用匹配项填充的输出参数,因此您可以简单地将匹配项的 strlen 用于模式 /^**/:

$matches = array();
preg_match("/^\**/", $string, $matches);
$result =  strlen($matches[0]) ;

...

"***Hello world!*" -> 3
"Hello world!" -> 0
于 2009-01-14T01:41:42.017 回答
2

这有点奇怪,但它可能会起作用——它计算第一个字符重复的次数:

strlen($line) - strlen(ltrim($line, $line[0]));

如果您只想从头开始删除所有星星,那么这会容易一些

strlen($line) - strlen(ltrim($line, '*'));
于 2009-01-14T01:37:18.780 回答