dongxing4196 2015-07-08 20:25
浏览 34
已采纳

php数组解析字符串时会遇到麻烦

I'm looping through an array of strings in PHP. Specifically, I'm looking for double dots in each array row (..). If no dots are found, the entire row is appended to a second array row. If it is found, then I split the row, put the left half in the current (second) array row, and the right half in the next row.

This is what I have so far:

$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;
    }
}

I keep getting undefined index after I increment $fudgedindex.

It's been a while since I've been on PHP, and I get the feeling that I'm either overthinking this and being a bit too procedural, or doing a head-slap error.

Any help appreciated! And once again, thanks for taking the time to help. =)

EDIT: Per the comments, here's what I'm aiming for...

$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.."

Result should be:

$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"

EDIT 2: The error is gone, so the initial issue is fixed. However, when I run this after:

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

Only the last row shows up.

  • 写回答

1条回答 默认 最新

  • douju1365 2015-07-08 20:33
    关注

    Your problem is with

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

    You've increased $fudgedindex and are setting a new array element with this statement. Yet you are also referencing this new element (the right side of the =) which doesn't exist yet - hense the error.

    You should just say

        $parsedrows[$fudgedindex] = $partialright;
    

    since it is the beginning of the next row.

    Your second problem starts with noticing you are reinitializing $parsedrows and wiping out all your previous array elements. You need to remove $parsedrows = '';

    However, because you can have a line with more than one "..", you need to include a loop to process each line until it is done. I had to rewrite the whole thing to accomplish this. This produces the result you want

    <?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>";
    }
    

    This produces (without the highlighting):

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b