PHP 获取字符串中每个第一个字符在数组中的位置
PHP get position of each first character in a string into an array
给定一个字符串,例如:
$string = " this is a string ";
什么是 return 一个 csv 数组的最佳方法,每个单词都包含一个数字,表示其第一个字符的位置,如下所示:
$string = " this is a string ";
^ ^ ^ ^
2 11 16 20
理想情况下,输出只是一个数组:
2,11,16,20
到目前为止,这是我所拥有的,但鉴于我的技能有限,我认为这有点难以理解:
$string = " this is a string ";
$string = rtrim($string); //just trim the right sides spaces
$len = strlen($string);
$is_prev_white = true;
$result = "";
for( $i = 0; $i <= $len; $i++ ) {
$char = substr( $string,$i,1);
if(!preg_match("/\s/", $char) AND $prev_white){
$result .= $i.",";
$prev_white = false;
}else{
$prev_white = true;
}
}
echo $result;
我得到:
2,4,11,16,20,22,24,26
您正在寻找的模式非常简单,不需要正则表达式来匹配它。您可以通过遍历字符串来做到这一点。
$l = strlen($string);
$result = array();
// use this flag to keep track of whether the previous character was NOT a space
$c = false;
for ($i=0; $i < $l; $i++) {
// if the previous character was a space and the current one isn't...
if (!$c && $string[$i] != ' ') {
// add current index to result
$result[] = $i;
}
// set the 'not a space' flag for the current character
$c = $string[$i] != ' ';
}
Php 正则表达式匹配为 return te 偏移量而不是匹配的子字符串提供了一个标志。使用以下代码段:
$hits = [];
preg_match_all("/(?<=\s)\w/", " this is a string ", $hits, PREG_PATTERN_ORDER | PREG_OFFSET_CAPTURE);
$result = array_column ( $hits[0], 1 );
$s_result = join ( ", ", $result);
echo $s_result;
正则表达式模式采用正向后视查找空白字符后的第一个字符。调用 array_column
从多维数组 returned 中提取结果数据作为模式匹配描述。 join
将数组元素连接成一个字符串,选择的分隔符将它变成一个 csv 行。
有关详细信息,请参阅 array_column and preg_match_all 的 php 文档。
实例here。根据此站点,该解决方案从 php 5.5.0.
起有效
您想要 PREG_OFFSET_CAPTURE 标志:
$string = " this is a string ";
preg_match_all('/(?:^|\s)([^\s])/', $string, $matches, PREG_OFFSET_CAPTURE);
$result = $matches[1];
echo var_dump($result);
正则表达式是:
(?:^|\s) // Matches white space or the start of the string (non capturing group)
(^\s) // Matches anything *but* white space (capturing group)
传递 PREG_OFFSET_CAPTURE 使 preg_match() 或 preg_match_all() return 匹配为包含匹配字符串和匹配索引的双元素数组搜索到的字符串。上面代码的结果是:
array(4) {
[0]=> array(2) { [0]=> string(1) "t" [1]=> int(2) }
[1]=> array(2) { [0]=> string(1) "i" [1]=> int(11) }
[2]=> array(2) { [0]=> string(1) "a" [1]=> int(16) }
[3]=> array(2) { [0]=> string(1) "s" [1]=> int(20) }
}
所以你可以用
得到只有索引的数组
$firstChars = array_column($result, 1);
简单,但进步 :) 具有preg_match_all
和array_walk
功能的解决方案:
将 preg_match_all
函数与 PREG_OFFSET_CAPTURE
标志一起使用:
PREG_OFFSET_CAPTURE : If this flag is passed, for every occurring match the appendant string offset will also be returned. Note that this changes the value of matches into an array where every element is an array consisting of the matched string at offset 0 and its string offset into subject at offset 1.
$string = " this is a string "; // subject
preg_match_all("/\b\w+\b/iu", $string, $matches, PREG_OFFSET_CAPTURE);
array_walk($matches[0], function(&$v){ // filter string offsets
$v = $v[1];
});
var_dump($matches[0]);
// the output:
array (size=4)
0 => int 2
1 => int 11
2 => int 16
3 => int 20
您也可以使用带有两个标志的 preg_split
。
$string = " this is a string ";
$flags = PREG_SPLIT_NO_EMPTY | PREG_SPLIT_OFFSET_CAPTURE;
// \W+ matches one or more non word characters
$csv = implode(",", array_column(preg_split('/\W+/', $string, -1, $flags), 1));
2,11,16,20
如果需要带偏移的字,去掉array_column
和implode
部分即可。
让我们在没有正则表达式的情况下试试这个。我希望它对你有用。
$str=" w this is a string ";
echo "<pre>";
print_r(first_letter_index($str));
function first_letter_index($str)
{
$arr2 = array_map('trim',str_split($str));
$result=array();
foreach($arr2 as $k=>$v)
{
if(!empty($v) && empty($arr2[$k-1]))
{
$result[$k]=$v;
}
}
return $result;
}
给定一个字符串,例如:
$string = " this is a string ";
什么是 return 一个 csv 数组的最佳方法,每个单词都包含一个数字,表示其第一个字符的位置,如下所示:
$string = " this is a string ";
^ ^ ^ ^
2 11 16 20
理想情况下,输出只是一个数组:
2,11,16,20
到目前为止,这是我所拥有的,但鉴于我的技能有限,我认为这有点难以理解:
$string = " this is a string ";
$string = rtrim($string); //just trim the right sides spaces
$len = strlen($string);
$is_prev_white = true;
$result = "";
for( $i = 0; $i <= $len; $i++ ) {
$char = substr( $string,$i,1);
if(!preg_match("/\s/", $char) AND $prev_white){
$result .= $i.",";
$prev_white = false;
}else{
$prev_white = true;
}
}
echo $result;
我得到: 2,4,11,16,20,22,24,26
您正在寻找的模式非常简单,不需要正则表达式来匹配它。您可以通过遍历字符串来做到这一点。
$l = strlen($string);
$result = array();
// use this flag to keep track of whether the previous character was NOT a space
$c = false;
for ($i=0; $i < $l; $i++) {
// if the previous character was a space and the current one isn't...
if (!$c && $string[$i] != ' ') {
// add current index to result
$result[] = $i;
}
// set the 'not a space' flag for the current character
$c = $string[$i] != ' ';
}
Php 正则表达式匹配为 return te 偏移量而不是匹配的子字符串提供了一个标志。使用以下代码段:
$hits = [];
preg_match_all("/(?<=\s)\w/", " this is a string ", $hits, PREG_PATTERN_ORDER | PREG_OFFSET_CAPTURE);
$result = array_column ( $hits[0], 1 );
$s_result = join ( ", ", $result);
echo $s_result;
正则表达式模式采用正向后视查找空白字符后的第一个字符。调用 array_column
从多维数组 returned 中提取结果数据作为模式匹配描述。 join
将数组元素连接成一个字符串,选择的分隔符将它变成一个 csv 行。
有关详细信息,请参阅 array_column and preg_match_all 的 php 文档。
实例here。根据此站点,该解决方案从 php 5.5.0.
起有效您想要 PREG_OFFSET_CAPTURE 标志:
$string = " this is a string ";
preg_match_all('/(?:^|\s)([^\s])/', $string, $matches, PREG_OFFSET_CAPTURE);
$result = $matches[1];
echo var_dump($result);
正则表达式是:
(?:^|\s) // Matches white space or the start of the string (non capturing group)
(^\s) // Matches anything *but* white space (capturing group)
传递 PREG_OFFSET_CAPTURE 使 preg_match() 或 preg_match_all() return 匹配为包含匹配字符串和匹配索引的双元素数组搜索到的字符串。上面代码的结果是:
array(4) {
[0]=> array(2) { [0]=> string(1) "t" [1]=> int(2) }
[1]=> array(2) { [0]=> string(1) "i" [1]=> int(11) }
[2]=> array(2) { [0]=> string(1) "a" [1]=> int(16) }
[3]=> array(2) { [0]=> string(1) "s" [1]=> int(20) }
}
所以你可以用
得到只有索引的数组$firstChars = array_column($result, 1);
简单,但进步 :) 具有preg_match_all
和array_walk
功能的解决方案:
将 preg_match_all
函数与 PREG_OFFSET_CAPTURE
标志一起使用:
PREG_OFFSET_CAPTURE : If this flag is passed, for every occurring match the appendant string offset will also be returned. Note that this changes the value of matches into an array where every element is an array consisting of the matched string at offset 0 and its string offset into subject at offset 1.
$string = " this is a string "; // subject
preg_match_all("/\b\w+\b/iu", $string, $matches, PREG_OFFSET_CAPTURE);
array_walk($matches[0], function(&$v){ // filter string offsets
$v = $v[1];
});
var_dump($matches[0]);
// the output:
array (size=4)
0 => int 2
1 => int 11
2 => int 16
3 => int 20
您也可以使用带有两个标志的 preg_split
。
$string = " this is a string ";
$flags = PREG_SPLIT_NO_EMPTY | PREG_SPLIT_OFFSET_CAPTURE;
// \W+ matches one or more non word characters
$csv = implode(",", array_column(preg_split('/\W+/', $string, -1, $flags), 1));
2,11,16,20
如果需要带偏移的字,去掉array_column
和implode
部分即可。
让我们在没有正则表达式的情况下试试这个。我希望它对你有用。
$str=" w this is a string ";
echo "<pre>";
print_r(first_letter_index($str));
function first_letter_index($str)
{
$arr2 = array_map('trim',str_split($str));
$result=array();
foreach($arr2 as $k=>$v)
{
if(!empty($v) && empty($arr2[$k-1]))
{
$result[$k]=$v;
}
}
return $result;
}