2016-06-27 132 views
1

HY我要搶例如每名:
我有數據
PHP切特定字符串

01.jpg02.jpg03.jpg10599335_899600036724556_4656814811345726851_n.jpg11693824_1051832718167953_6310308040295800037_n.jpg11709788_1051835281501030_8503525152567309473_n.jpg12042832_1103685106316047_3711793359145824637_n.jpg 

我嘗試這樣,但不是爲我工作

$str = $_POST['name']; 
print_r (explode(".jpg", $str)); 
foreach ($str as $key => $value) { 
echo $value.'<br>'; 
} 

enter image description here

+0

你想抓住與擴展,比如'10599335_899600036724556_4656814811345726851_n.jpg'文件? – RomanPerekhrest

+0

您的預期產量是多少? – anubhava

+0

@RomanPerekhrest是的,這是怎麼回事 – b4dQuetions

回答

0

使用preg_match_all功能的解決方案:

$str = "01.jpg02.jpg03.jpg10599335_899600036724556_4656814811345726851_n.jpg11693824_1051832718167953_6310308040295800037_n.jpg11709788_1051835281501030_8503525152567309473_n.jpg12042832_1103685106316047_3711793359145824637_n.jpg"; 

preg_match_all("/(?<=\.jpg)?\w+\.jpg/", $str, $matches); 

print_r($matches[0]); 

輸出:

Array 
(
    [0] => 01.jpg 
    [1] => 02.jpg 
    [2] => 03.jpg 
    [3] => 10599335_899600036724556_4656814811345726851_n.jpg 
    [4] => 11693824_1051832718167953_6310308040295800037_n.jpg 
    [5] => 11709788_1051835281501030_8503525152567309473_n.jpg 
    [6] => 12042832_1103685106316047_3711793359145824637_n.jpg 
) 
0

您可以在preg_match_all中使用這個lookbehind正則表達式:

/(?<=^|\.jpg)\w+\.jpg/ 

Lookbehind斷言我們的匹配具有前面的.jpg或行開頭。

RegEx Demo

代碼:

$re = '/(?<=^|\.jpg)\w+\.jpg/m'; 

preg_match_all($re, $input, $matches); 

print_r($matches[0]); 

Code Demo

0

爆炸將分隔值關閉,因此您需要重新附加它。

$filenames = explode('.jpg', '01.jpg02.jpg03.jpg10599335_899600036724556_4656814811345726851_n.jpg11693824_1051832718167953_6310308040295800037_n.jpg11709788_1051835281501030_8503525152567309473_n.jpg12042832_1103685106316047_3711793359145824637_n.jpg'); 
foreach($filenames as $file) { 
    if(!empty($file)) { 
     echo $file . ".jpg\n"; 
    } 
} 

https://eval.in/596532

一個正則表達式的方法,我認爲會的工作是:

(.*?\.jpg) 

正則表達式演示:https://regex101.com/r/gR7rC5/1

PHP:

preg_match_all('/(.*?\.jpg)/', '01.jpg02.jpg03.jpg10599335_899600036724556_4656814811345726851_n.jpg11693824_1051832718167953_6310308040295800037_n.jpg11709788_1051835281501030_8503525152567309473_n.jpg12042832_1103685106316047_3711793359145824637_n.jpg', $filenames); 
foreach($filenames[1] as $file) { 
    echo $file . "\n"; 
} 

演示:https://eval.in/596530