dougua9328 2013-07-20 00:38
浏览 31
已采纳

如何在php中操作复杂的字符串?

I am trying to group bunch of texts from a string and create an array for it.

The string is something like this:

<em>string</em>  and the <em>test</em> here.  
tableBegin rowNumber:2, columnNumber:2  11 22 33 44 tableEnd  
<em>end</em> text here

I was hoping to get an array like the following results

array (0 => '<em>string</em>  and the <em>test</em> here.',
         1=>'rowNumber:5',
         2=>'columnNumber:3',
         3=>'11',
         4=>'22',
         5=>'33',
         6=>'44'
         7=>'<em>end</em> text here')

11,22,33,44 are the table cell data the user enters. I want to make them have unique index but keep the rest of texts together.

tableBegin and tableEnd are just the check for the table cell data

Any help or tips? Thanks a lot!

  • 写回答

2条回答 默认 最新

  • douhuiyan2772 2013-07-20 03:08
    关注

    You may try the following, note that you need PHP 5.3+:

    $string = '<em>string</em>  and the <em>test</em> here.  
    tableBegin rowNumber:2, columnNumber:2  11 22 33 44 tableEnd
    SOme other text
    tableBegin rowNumber:3, columnNumber:3  11 22 33 44 55 tableEnd
    <em>end</em> text here';
    
    $array = array();
    preg_replace_callback('#tableBegin\s*(.*?)\s*tableEnd\s*|.*?(?=tableBegin|$)#s', function($m)use(&$array){
        if(isset($m[1])){ // If group 1 exists, which means if the table is matched
            $array = array_merge($array, preg_split('#[\s,]+#s', $m[1])); // add the splitted string to the array
          // split by one or more whitespace or comma --^
        }else{// Else just add everything that's matched
            if(!empty($m[0])){
                $array[] = $m[0];
            }
        }
    }, $string);
    print_r($array);
    

    Output

    Array
    (
        [0] => string  and the test here.  
    
        [1] => rowNumber:2
        [2] => columnNumber:2
        [3] => 11
        [4] => 22
        [5] => 33
        [6] => 44
        [7] => SOme other text
    
        [8] => rowNumber:3
        [9] => columnNumber:3
        [10] => 11
        [11] => 22
        [12] => 33
        [13] => 44
        [14] => 55
        [15] => end text here
    )
    

    Regex explanation

    • tableBegin : match tableBegin
    • \s* : match a whitespace zero or more times
    • (.*?) : match everything ungreedy and put it in group 1
    • \s* : match a whitespace zero or more times
    • tableEnd : match tableEnd
    • \s* : match a whitespace zero or more times
    • | : or
    • .*?(?=tableBegin|$) : match everything until tableBegin or end of line
    • The s modifier : make dots also match newlines
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行