duaner1634 2015-11-11 08:28
浏览 48

如何在字符串php中关闭几个空格后添加多个管道,逗号等[关闭]

Can anyone please help me regarding this issue, i tried alot regexp and substr too but im unable to figure it out. My requirement is to add | sign in string after 1, 28, 48, 62 and so on! im converting a text file into psv format for our crm, can anybody help me regarding this? Regards

$final = preg_replace("/^(.{0})/", "$1|", $line[0]);

this code is matching only one position per string, i want to replace to 6-10 specific columns, i tried this too but not working

$final = preg_replace("/^(.{0}{28}{48})/", "$1| $2| $3|", $line[0]);

but my solution in this expression if it works one more example input:

$string = "80293066640226135465        NRIC                011064608711  HAZRY BIN ABDULLAH                                                                                                                                    0138258856                                                                                                                                                                                                                                                                                                                                                                                                                     LOT 7083,LORONG 6B1 JLN MATANG                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                TAMAN MATANG JAYA                                                                                                       93050,KUCHING,SAR                                           93050               SAR                                     ABO TAYANGAN HEBAT FOR VALUE PACK,ABO TAYANGAN HEBAT HD,AOTG + CATCH UPMALAY                         URB4929                1                   2015090404/09/2015      04/11/2015      138.0000                                          BASIC FAMILY HD PACKAGE,HD SERVICES FOR VALUE PACK,RECORDING (XTV) FOR                     

"; desired output:

80293066|640226135465|NRIC|011064608711|HAZRY BIN ABDULLAH||0138258856||||$ $ $LOT 7083,LORONG 6B1 JLN MATANG$|TAMAN MATANG JAYA $93050$,KUCHING,SAR $SAR $||ABO TAYANGAN HEBAT FOR VALUE PACK,ABO TAYANGAN HEBAT HD,AOTG + CATCH UP|MALAY|URB4929||1|20150904|04/09/2015|04/11/2015|138.0000||BASIC FAMILY HD PACKAGE,HD SERVICES FOR VALUE PACK,RECORDING (XTV) FOR|||

so some fields will have | sign and some will have $.

  • 写回答

3条回答 默认 最新

  • dqwnxdhb88531 2015-11-11 09:32
    关注

    Should every row have fixed size per column (first column 10 characters, second 20 characters etc, notice here the diference between fixed and max, every single column must have the same length exactly

    function char_replace($input , $replace , $position)
            {
                if(is_array($position))
                {
                    $output = $input;
                    foreach($position as $character_to_change)
                    {
                        $tmp = substr($output , 0 , $character_to_change -1);
                        $tmp .= $replace;
                        $tmp .= substr($output , $character_to_change );
                        $output = $tmp;
                    }
                    return $output;
                }
                else
                {
                    $output = substr($input , 0 , $position -1);
                    $output .= $replace;
                    $output .= substr($input , $position );
                    return $output;
                }
            }
            //example with array
            $string = "12345";
            echo char_replace($string,"|",array(2,4));
                //example
                $string = "abcdef";
                echo char_replace($string,"|",2);
    

    this function could help you well. do something like this: $result= char_replace(char_replace($input,"|",48),"|",12); or just run it with a for

    EDIT: working with an array for multiple position as well as single int now

    评论

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码