【发布时间】:2014-09-08 01:13:13
【问题描述】:
我在 php 中得到一个这样的字符串值 -
How are you|1 I am fine|2 That is fine|3
我想将这个字符串分解为这个整数值的分隔符。基本上我需要这三个值。
How are you|1
I am fine|2
That is fine|3
任何人都可以建议我该怎么做。
【问题讨论】:
标签: php substring explode delimiter
我在 php 中得到一个这样的字符串值 -
How are you|1 I am fine|2 That is fine|3
我想将这个字符串分解为这个整数值的分隔符。基本上我需要这三个值。
How are you|1
I am fine|2
That is fine|3
任何人都可以建议我该怎么做。
【问题讨论】:
标签: php substring explode delimiter
尝试使用带有一些正则表达式的preg_split() 而不是explode():
$arr = preg_split('#(?<=\|\d )(?=[a-z])#i', "How are you|1 I am fine|2 That is fine|3");
这将拆分字符串,其中有一个管道字符 | 后跟一个数字,后跟一个空格。
编辑
如果空格可以是换行符或回车符,请在其中添加 or 条件:
$arr = preg_split('#(?<=\|\d( |\n|\r))(?=[a-z])#i', "How are you|1 I am fine|2 That is fine|3");
【讨论】:
str_replace(array("\r", "\n"), "", $str)。或者稍微改变一下你的正则表达式:#(?<=\|\d\n)(?=[a-z])#i
$arr = array_map(function($v){ return $v."\n"; }, $arr)
$str = 'How are you|1 I am fine|2 That is fine|3';
$pattern = '/(\D+)(\d+)/';
$res = preg_match_all($pattern, $str, $matches);
print_r($matches[0]);
【讨论】:
你可以使用65Fbef05的技巧
你的情况是
$str = 'How are you|1 I am fine|2 That is fine|3';
$del = array(1, 2, 3);
// In one fell swoop...
$arr = explode( $del[0], str_replace($del, $del[0], $str) );
【讨论】:
尝试使用 chr(13) 作为分隔符进行爆炸。
试试这个循环。
$extras = $db->loadRowList();
$num=sizeof($extras);
for($i=0;$i<$num;$i++)
{
echo "<br>".$extras[$i][0]."<br>";
$arr=explode(chr(13), $extras[$i][0]);
$num1=sizeof($arr);
for($j=0;$j<$num1;$j++)
{
$tmp = explode('|', $arr[$j]);
$num2=sizeof($tmp);
for($k=0;$k<$num2;$k++)
{
echo $tmp[$k]."<br>";
}
}
}
【讨论】: