分割字符串,排除某些字符

发布于 2024-12-22 06:34:51 字数 1008 浏览 2 评论 0原文

可能的重复:
按分隔符拆分字符串,但如果转义则不拆分< /a>

我有一个从 ibm informix 数据库生成的字符串,该字符串由管道 | 字符分隔,并且存在一些数据错误,这意味着里面有 反斜杠 + 管道 数据。我只想从管道符号中拆分这些字符串,而不是从 反斜杠 + 管道 \| 或其他管道符号中拆分这些字符串。

这是我的代码,但它仅适用于管道字符:

foreach(glob("ssbstat.unl") as $file)
{ 
    $c=0;       
    if(($load = fopen($file, "r")) !== false)
    { 
        $line = fgets($load);           
        $count= count(explode('|', $line));
        echo $fm= str_repeat('%[^|]|', $count)."%s\n";      

        do
        {
            echo $line;
            print_r($line);
            if($c++>10) break;
        } while ($line = fscanf($load, $fm));
    }
}

任何人都可以帮助我做到这一点吗?

Possible Duplicate:
Split string by delimiter, but not if it is escaped

I have a string generated form ibm informix database which is separated by pipe | characters and there are some data errors, which means there are backslash + pipe inside the data. I want to split these strings only from the pipe sign, not from backslash + pipe \| or other signs with the pipe.

This is my code, but it works only for the pipe character:

foreach(glob("ssbstat.unl") as $file)
{ 
    $c=0;       
    if(($load = fopen($file, "r")) !== false)
    { 
        $line = fgets($load);           
        $count= count(explode('|', $line));
        echo $fm= str_repeat('%[^|]|', $count)."%s\n";      

        do
        {
            echo $line;
            print_r($line);
            if($c++>10) break;
        } while ($line = fscanf($load, $fm));
    }
}

Can anyone help me do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

玩物 2024-12-29 06:34:51

这样做:

<?php
$line = preg_replace("/([^\\\])\|/", "$1 |", "Hi \|error\| man|ok man|perfect man");
print_r(preg_split('/[^\\\]\|/', $line));

将输出:

Array ( [0] => "Hi \|error\| man" [1] => "ok man" [2] => "perfect man" )

Testet!

编辑:就像Maerlyn所说,这也是可能的:

<?php
$line = "Hi \|error\| man|ok man|perfect man";
print_r(preg_split('~\\\\.(*SKIP)(*FAIL)|\|~s', $line));

Do it like this:

<?php
$line = preg_replace("/([^\\\])\|/", "$1 |", "Hi \|error\| man|ok man|perfect man");
print_r(preg_split('/[^\\\]\|/', $line));

Will output:

Array ( [0] => "Hi \|error\| man" [1] => "ok man" [2] => "perfect man" )

Testet!

Edit: Like Maerlyn said, this is also possible:

<?php
$line = "Hi \|error\| man|ok man|perfect man";
print_r(preg_split('~\\\\.(*SKIP)(*FAIL)|\|~s', $line));
耳根太软 2024-12-29 06:34:51

您可以使用 preg_split 来完成此操作。这一段 [^\\\\] 指定应忽略带有反斜杠的管道(正确转义需要四个反斜杠。您可以在 [ 内添加您想要忽略的任何其他字符]

print_r(preg_split('/(?<![\\\\])\|/', 'This\|is a|test|string'));

You can do this with preg_split. This piece [^\\\\] specifies that pipes with backslashes should be ignored (the four backslashes are required for proper escaping. You can add any other character you want to ignore inside the [].

print_r(preg_split('/(?<![\\\\])\|/', 'This\|is a|test|string'));
浅笑轻吟梦一曲 2024-12-29 06:34:51

将 backslah + pipelinesign 替换为占位符,然后通过 pipelinesign 分解,然后用 backslah + pipelinesign 替换后面的占位符

Replace backslah + pipesign with a placeholder, then explode by pipesign, then replace back placeholder with backslah + pipesign

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文