Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将字符串分解为单独的子字符串_Php_String - Fatal编程技术网

Php 如何将字符串分解为单独的子字符串

Php 如何将字符串分解为单独的子字符串,php,string,Php,String,这些只是实际结构的例子 如何使用substr()和数组[$x]变量将$source拆分为单个字符串? $startIndex和$endIndex保证大小相同 这似乎不起作用 $source = "oasdfyoasdfyoasdfyoasdfy"; $startIndexes = {0, 6, 12, 18}; #(o characters) $endIndexes = {5, 11, 17, 23}; #(y characters) for($x=0;$x

这些只是实际结构的例子

如何使用substr()和数组[$x]变量将$source拆分为单个字符串? $startIndex和$endIndex保证大小相同

这似乎不起作用

$source = "oasdfyoasdfyoasdfyoasdfy";
$startIndexes = {0, 6, 12, 18}; #(o characters)
$endIndexes = {5, 11, 17, 23}; #(y characters)
for($x=0;$x
'; }

我知道数组没有正确初始化,它们只是为了显示真实数组的样子。

我相信您无法以这种方式初始化数组。应该是

for($x = 0; $x < sizeOf($startIndexes); $x++)
{
    echo substr($source, $startIndexes[$x], $endIndexes[$x] - $startIndexes[$x]) . '</br></br>';
}

数组init错误,缺少misterious 1请参见wiki

$source=“oasdfyoasdfyoasdfyoasdfy”;
$startIndexes=数组(0,6,12,18)#(o个字符)
$endIndexes=数组(5,11,17,23)#(y个字符)
对于($x=0;$x”;
}

这里有一个稍微不同的方法,使您的代码更小、更安全、更易于阅读和更快:

$source = "oasdfyoasdfyoasdfyoasdfy";

$startIndexes = array(0, 6, 12, 18); #(o characters)
$endIndexes =  array(5, 11, 17, 23); #(y characters)

for($x = 0; $x < count($startIndexes); $x++) {

    echo substr($source, $startIndexes[$x], $endIndexes[$x] - $startIndexes[$x] + 1 ) . '</br></br>';

}
//您的字符串
$source=“oasdfyoasdfyoasdfyoasdfy”;
//使用起始位置,您只能有一个数组,而不是两个数组
//作为关键点,结束位置作为值
$positions=数组(0=>5,6=>11,12=>17,18=>23);
//做一个foreach循环,效率更高。
foreach($开始=>$结束位置)
{
echo substr($source、$start、$end-$start+1)。“

”; }
首先,如果可能,请始终提供实际的代码示例。否则我们就只能说“我写了一些不起作用的东西”。我们只能用“写一些有用的东西”来回答

数组语法应该是
$startIndex=array(0,6,12,18)

其次,不需要第二个数组

看一看



您是否收到任何错误消息?像是分配变量的方式出现了解析错误?看起来第三个substr参数(长度)缺少+1。假设你想把连续的“oasdfy”串分开。substr的第三个参数是长度,因此您的开始位置是正确的,但长度是结束索引减去开始索引加1。我必须承认我不是一名PHP程序员,所以正如其他人评论的那样,您的PHP编码可能存在一些问题。我看到您最近经历了一场激烈的洗礼——删除了一个温和的问题,我认为您是自愿删除了自己的一个问题?不要担心被删除的问题:回到马上,问更多的问题!对于新用户,我建议在提问之前彻底研究问题;形式为“此代码有什么问题”的问题比“我该如何做”的问题要好得多。最后,如果高代表性的用户给你建议,通常是善意的——即使这意味着有些问题并不像你想的那样与主题相关。祝你好运!的确,这不是最好的时机,但最终我意识到我的不耐烦。我在大约一天的时间内解决了我贴出的问题,当我本应该为自己探索和学习的时候,我真的很想得到答案。“我将来会问更彻底、更深思熟虑的问题。”德里克还对“一”问题发表了评论,我在发布答案时没有看到这个问题。
$source = "oasdfyoasdfyoasdfyoasdfy";

$startIndexes = array(0, 6, 12, 18); #(o characters)
$endIndexes =  array(5, 11, 17, 23); #(y characters)

for($x = 0; $x < count($startIndexes); $x++) {

    echo substr($source, $startIndexes[$x], $endIndexes[$x] - $startIndexes[$x] + 1 ) . '</br></br>';

}
// Your string
$source = "oasdfyoasdfyoasdfyoasdfy";
// Instead of two arrays, you can have only one, using start positions 
// as the keys, and end positions as values
$positions = array(0=>5, 6=>11, 12=>17, 18=>23);
// Do a foreach loop, it's more efficient.
foreach($positions as $start => $end)
{
    echo substr($source, $start, $end - $start + 1) . '</br></br>';
}
<?php
    function suffix($number){
        $suffix = array('th','st','nd','rd','th','th','th','th','th','th');
        if (($number %100) >= 11 && ($number%100) <= 13)
            $abbreviation = $number. 'th';
        else
            $abbreviation = $number. $suffix[$number % 10];
        return $abbreviation;
    }
$source = "oasdfyoasdfyoasdfyoasdfy";
$startIndexes =array(0, 6, 12, 18);

for ($i=0; $i < count($startIndexes); $i++){
    $index= $startIndexes[$i];
    $len = ($i< count($startIndexes)-1 ? $startIndexes[$i +1]  :
                                          strlen($source)) - ($index);
    echo sprintf("The %s substring is:[%s]\n",
                                              suffix($i+1),
                                              substr($source, $index, $len));
 }

?>