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 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,
  • ¥15 spaceclaim模型变灰色
  • ¥15 求一份华为esight平台V300R009C00SPC200这个型号的api接口文档