简体   繁体   English

php的preg_match_all:按第一个字符分割字符串

[英]php's preg_match_all: split string by first character

I'd like to use PHP's preg_match_all function to split a string by a certain caracter, while the 'certain character' is the first character in the string, eg: 我想使用PHP的preg_match_all函数通过特定字符分割字符串,而“某些字符”是字符串中的第一个字符,例如:

$pattern = '/#(?P<word>[.....])';
$string = 'one#two#three';
$matches = array();
preg_match_all($pattern,$string, $matches);
// expected $matches array: $matches['word'] = 'one','two','three'

So the "split field character" is itself part of the pattern (the first char). 因此,“分割字段字符”本身就是模式的一部分(第一个字符)。

Is this even possible? 这有可能吗? if yes, how? 如果是,如何?

为什么不使用explode

$matches = explode('#', $string);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM