PHP剪切特定字符串

时间:2016-06-27 15:54:31

标签: php regex

hy我想抓住每个文件名例如:
我有数据

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

4 个答案:

答案 0 :(得分:0)

您可以在private dummyData = { json: function() { return [ { id: 1, title: 'Title 1', content: 'content 1', author: 'author 1' }, { id:2, title: 'Title 2', content: 'content 2', author: 'author 1' } ]}; } 中使用此lookbehind正则表达式:

preg_match_all

Lookbehind声称我们的匹配前面有/(?<=^|\.jpg)\w+\.jpg/ 或行开头。

RegEx Demo

<强>代码:

.jpg

Code Demo

答案 1 :(得分: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
)

答案 2 :(得分: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

答案 3 :(得分:0)

{{1}}

点击preg_match_all
http://www.phpliveregex.com/p/gcR