duanchijie2323 2017-06-22 12:18
浏览 43

PHP解析文本区域中的特定字符串

I have been working on a PHP webpage to help me parse lots of data. It works but I would like it more robust .

So on my main page I have a text area that you can copy in as much information as you want. You click submit posts the data into a page that processes it

here is the code currently that parses it

 foreach ($textAr as $line) 


    if (substr( $line, 0, 2 ) == "IN") 
    {
        $line2 = $line;
        $textAr2 = explode("", $line2);
            $textAr2 = array_filter($textAr2, 'trim'); // remove any extra characters left behind
        foreach ($textAr2 as $line2) 
            {
                $length = strlen($line2);
                if ( $length == 10)
                {
                    if (substr( $line2, 0, 2 ) == "IN") 
                    {
                        $count++;
                        $line3 = ltrim($line2, 'IN');

                        echo $line3 . ",";
                    }

                }


            }
    }

the data that I input always has a format of

names   dates times summary stuffs      infostuffs  stats   names   dates   IN*somenumber
names    dates  times summary stuffs        infostuffs          names   dates   IN*somenumber
names   dates times summary stuffs      infostuffs  stats   names   dates   IN*somenumber
names   dates times summary stuffs      infostuffs  stats       dates   IN*somenumber
names   dates times summary stuffs      infostuffs  stats   names   dates   IN*somenumber
names   dates times summary stuffs      infostuffs  stats   names   dates   IN*somenumber
 names  dates times summary stuffs      infostuffs  stats   names   None    IN*somenumber
names   dates times summary stuffs      infostuffs  stats   names   dates   IN*somenumber

This works great, I know its not the most efficient but it works. However I want to make it more robust and I'm failing due to lack of coding knowledge. It is always going to be searching for a string that starts with "IN" and has 8 numbers after it. After that it strips the IN out and put it into a continuous string of numbers like 88888888,88888888,88888888,88888888,88888888,88888888, The , is critical as I use it for csv stuff

I want it to handle other formats here are a few examples are some things that it can't handle that I want it to from above if I add another "IN*somenumber" at the end of the format it doesn't parse it

names   dates times summary stuffs      infostuffs  stats   names   dates   IN88888888 IN88888888 

in the above example all it would return is "88888888," completely missing the second one

Other item I would want it to parse is

IN88888888 
IN88888888 
IN88888888 
IN88888888 
IN88888888 
IN88888888 

when it finishes the output is

88888888,
IN88888888 
IN88888888 
IN88888888 
IN88888888 
IN88888888

Any help or suggestions would be great I have been trying to search on w3schools for like just a generic search function with wild cards but have not came up with any here is what I think would be ideal

do util end of string(Posted textarea)
   $output = search("IN" . any8numbers);
   echo $output .  ",";

Edit1: Based on suggestion here are some things I have done I tried using a strrpos() as its case insensative but not sure its exactly what I need as it only finds the first or last occurrence (based on what version you use) I have also tried to use more explode into array and that gets complicated quickly. I believe it is making the problem worse with that as it makes it harder to change in the future if I want to change contents because each explode is relying on the above ones to parse correctly. If it doesn't then it all gets messed up.

I ususally debug this code with basic echos as company laptop will only allow notepad ++ and the PHP server gives no logs and I have no access to it besides storing and running pages

Maybe I should take the text area explode it into an array of individual strings then test if each item in the arry starts with 'IN' then strip it.

Other suggestions?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀
    • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
    • ¥15 关于#hadoop#的问题
    • ¥15 (标签-Python|关键词-socket)
    • ¥15 keil里为什么main.c定义的函数在it.c调用不了
    • ¥50 切换TabTip键盘的输入法