分割字符串,排除某些字符
可能的重复:
按分隔符拆分字符串,但如果转义则不拆分< /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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这样做:
将输出:
Testet!
编辑:就像Maerlyn所说,这也是可能的:
Do it like this:
Will output:
Testet!
Edit: Like Maerlyn said, this is also possible:
您可以使用
preg_split
来完成此操作。这一段[^\\\\]
指定应忽略带有反斜杠的管道(正确转义需要四个反斜杠。您可以在[ 内添加您想要忽略的任何其他字符]
。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[]
.将 backslah + pipelinesign 替换为占位符,然后通过 pipelinesign 分解,然后用 backslah + pipelinesign 替换后面的占位符
Replace backslah + pipesign with a placeholder, then explode by pipesign, then replace back placeholder with backslah + pipesign