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 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘