我在PHP中得到一個字符串值,這樣的 -如何使用整數值爆炸作爲分隔符
How are you|1 I am fine|2 That is fine|3
我想爆炸這個字符串分隔符服用這種整數值。基本上我需要這3個值。
How are you|1
I am fine|2
That is fine|3
任何人都可以建議我能做些什麼。
我在PHP中得到一個字符串值,這樣的 -如何使用整數值爆炸作爲分隔符
How are you|1 I am fine|2 That is fine|3
我想爆炸這個字符串分隔符服用這種整數值。基本上我需要這3個值。
How are you|1
I am fine|2
That is fine|3
任何人都可以建議我能做些什麼。
嘗試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>";
}
}
}
您可以使用從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));
感謝您的回覆Bruzetti。不過,我不能在你的數據庫中使用你的代碼bcz,那些整數並不總是按順序遞增。但我給你+1,因爲這段代碼是非常正確的。 –
爲什麼你需要使用一個整數分隔符?特別是因爲它們已經按時間順序排列了?
只是這樣做:
$string = "How are you| I am fine| That is fine"; $arr = explode('|', $string); var_dump($arr);
嘗試使用preg_split()
一些正則表達式,而不是explode()
:
$arr = preg_split('#(?<=\|\d)(?=[a-z])#i', "How are you|1 I am fine|2 That is fine|3");
這將拆分那裏有一個豎線串|後跟一個數字,後跟一個空格。
編輯
如果空間可能是新行或回車,在裏面添加一個或條件:
$arr = preg_split('#(?<=\|\d(|\n|\r))(?=[a-z])#i', "How are you|1 I am fine|2 That is fine|3");
你的代碼工作正常,但我在這裏遇到了一個新問題。請檢查我的答案。 –
@ gsm11001您可以簡單地在手前將所有換行符str_replace(array(「\ r」,「\ n」),「」,$ str)'替換掉。或者改變你的正則表達式:'#(?<= \ | \ d \ n)(?= [a-z])#i' – George
我不能替換換行符,因爲它也會修改插件。然而,在我從數據庫中獲取的值中,我無法看到任何換行符代碼。也改變正則表達式並沒有這樣做。 –
$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]);
它爲我工作非常感謝 –
非常感謝。有效。這是(13),這是造成這個問題。 –