分析字符串

时间:2015-07-08 20:25:40

标签: php arrays

我在PHP中循环遍历一个字符串数组。具体来说,我在每个数组行(..)中寻找双点。如果未找到任何点,则整行将附加到第二个阵列行。如果找到了,那么我拆分行,将左半部分放在当前(第二个)数组行中,将右半部分放在 next 行中。

这是我到目前为止所做的:

$fudgedindex = 0;
$parsedrows = array('');

foreach($refinedlist as $rowtoparse)
{
    $pos2 = strpos($rowtoparse, "..");

    //echo $refinedlist[$index] . "<br>";
    if ($pos2 === false)
    {
        $parsedrows[$fudgedindex] = $parsedrows[$fudgedindex] . $rowtoparse;
    }
    else
    {
        $length = strlen($rowtoparse);
        $partialleft = substr($rowtoparse, 0, $pos2);
        $partialright = substr($rowtoparse, $pos2);
        $parsedrows[$fudgedindex] = $parsedrows[$fudgedindex] . $partialleft;
        $fudgedindex = $fudgedindex + 1;
        $parsedrows = '';

        $parsedrows[$fudgedindex] = $parsedrows[$fudgedindex] . $partialright;
    }
}

在我增加$ fudgedindex后,我一直得到未定义的索引。

自从我使用PHP以来已经有一段时间了,我感觉我要么过度思考这个过程,要么过于程序化,或者做一个头脑错误。

任何帮助表示赞赏!再次感谢您抽出宝贵时间提供帮助。 =)

编辑:根据评论,这里是我的目标...

$refinedlist[0]="Today I bought milk..Then I went to the "
[1]="store and bought even more food "
[2]="such as apples..I was happy..Then"
[3]=" I went to stackoverflow.com and asked this question "
[4]="to solve my problem.."

结果应该是:

$parsedrows[0]="Today I bought milk"
[1]="Then I went to the store and bought even more food such as apples"
[2]="I was happy."
[3]="Then I went to stackoverflow.com and asked this question to solve my problem"

编辑2:错误消失了,所以最初的问题是固定的。但是,当我在此之后运行时:

foreach($parsedrows as $rowtoprint)
{
    echo $rowtoprint . "<br>";
}

只显示最后一行。

1 个答案:

答案 0 :(得分:1)

你的问题在于

    $parsedrows[$fudgedindex] = $parsedrows[$fudgedindex] . $partialright;

您已经增加$fudgedindex并使用此语句设置新的数组元素。然而你也引用了这个新元素(=的右边),它还不存在 - 这个错误。

你应该说

    $parsedrows[$fudgedindex] = $partialright;

因为它是下一行的开头。

您的第二个问题始于注意到您正在重新初始化$ parsedrows并清除所有以前的数组元素。您需要删除$parsedrows = '';

但是,因为您可以拥有一个包含多个“..”的行,所以您需要包含一个循环来处理每一行,直到完成为止。我不得不重写整个事情来实现这一目标。这会产生你想要的结果

<?php

$refinedlist[0]="Today I bought milk..Then I went to the ";
$refinedlist[1]="store and bought even more food ";
$refinedlist[2]="such as apples..I was happy..Then";
$refinedlist[3]=" I went to stackoverflow.com and asked this question ";
$refinedlist[4]="to solve my problem..";

$fudgedindex = 0;
$parsedrows = array('');

foreach($refinedlist as $rowtoparse)   
{
    while (($pos2 = strpos($rowtoparse, "..")) !== false)
    {
        $length = strlen($rowtoparse);
        $partialleft = substr($rowtoparse, 0, $pos2);

        // keep what is not yet processed in the original var
        // while loop will process again
        $rowtoparse = substr($rowtoparse, $pos2 + 2);

        // add to current row
        $parsedrows[$fudgedindex] = $parsedrows[$fudgedindex] . $partialleft;

        // increment the row & initialize
        $fudgedindex += 1;
        $parsedrows[$fudgedindex] = "";
    }

    // add whatever is left to the current row
    $parsedrows[$fudgedindex] = $parsedrows[$fudgedindex] . $rowtoparse;
}

foreach($parsedrows as $rowtoprint)
{
    echo $rowtoprint . "<br>";
}

这会产生(没有突出显示):

Today I bought milk
Then I went to the store and bought even more food such as apples
I was happy
Then I went to stackoverflow.com and asked this question to solve my problem