我有这个模式,我用它来提取/ image /字段后面的数字,我已经尝试过这个模式,我已经在http://www.functions-online.com/preg_match_all.html在线查看了它,它为第一个链接提供了所需的输出但对于其他链接,它没有提供所需的输出 这是我的模式
/\sample.com\/image\/(.*)\//
这是我的字符串
Mario Ermito photos by sample.com Mario Ermito Latest News, Photos, Biography, Videos and Wallpapers [img]http://xyz.sample.com/image/4205476/600full-mario-ermito.jpg[/img][img]http://xyz.sample.com/image/4453948/600full-my-profile.jpg[/img][img]http://xyz.sample.com/image/427185/600full-eagle-eye-poster.jpg[/img][img]http://xyz.sample.com/image/1323868/600full-alexis-bledel.jpg[/img][img]http://xyz.sample.com/image/2505314/600full-monroe-lee.jpg[/img][img]http://xyz.sample.com/image/3300481/600full-cindy-crawford.jpg[/img][img]http://xyz.sample.com/image/1046646/600full-pitura-freska.jpg[/img][img]http://xyz.sample.com/image/4322305/600full-kristin-kreuk.jpg[/img][img]http://xyz.sample.com/image/4261476/600full-kang-so--ra.jpg[/img][img]http://xyz.sample.com/image/3386911/600full-summer-brielle.jpg[/img][img]http://xyz.sample.com/image/4663949/600full-the-closer-artwork.jpg[/img]
例如
我想在/ image / field之后只提取数字我不想要图像名称我想要的输出是
4205476
4453948
427185
等字符串
中的所有数字答案 0 :(得分:1)
使用此正则表达式~\/\image\/(.*?)\/~
<?php
$str=' Mario Ermito photos by sample.com Mario Ermito Latest News, Photos, Biography, Videos and Wallpapers [img]http://xyz.sample.com/image/4205476/600full-mario-ermito.jpg[/img][img]http://xyz.sample.com/image/4453948/600full-my-profile.jpg[/img][img]http://xyz.sample.com/image/427185/600full-eagle-eye-poster.jpg[/img][img]http://xyz.sample.com/image/1323868/600full-alexis-bledel.jpg[/img][img]http://xyz.sample.com/image/2505314/600full-monroe-lee.jpg[/img][img]http://xyz.sample.com/image/3300481/600full-cindy-crawford.jpg[/img][img]http://xyz.sample.com/image/1046646/600full-pitura-freska.jpg[/img][img]http://xyz.sample.com/image/4322305/600full-kristin-kreuk.jpg[/img][img]http://xyz.sample.com/image/4261476/600full-kang-so--ra.jpg[/img][img]http://xyz.sample.com/image/3386911/600full-summer-brielle.jpg[/img][img]http://xyz.sample.com/image/4663949/600full-the-closer-artwork.jpg[/img]';
preg_match_all('~\/\image\/(.*?)\/~', $str, $matches);
print_r($matches[1]);
<强> OUTPUT :
强>
Array
(
[0] => 4205476
[1] => 4453948
[2] => 427185
[3] => 1323868
[4] => 2505314
[5] => 3300481
[6] => 1046646
[7] => 4322305
[8] => 4261476
[9] => 3386911
[10] => 4663949
)
答案 1 :(得分:1)
您需要调整正则表达式:
$regex = '@sample\.com/image/([0-9]+)/@'
preg_match_all('@sample\.com/image/([0-9]+)/@', $str, $m);
print_r($m);
预期产出:
Array
(
[0] => Array
(
[0] => sample.com/image/4205476/
[1] => sample.com/image/4453948/
[2] => sample.com/image/427185/
[3] => sample.com/image/1323868/
[4] => sample.com/image/2505314/
[5] => sample.com/image/3300481/
[6] => sample.com/image/1046646/
[7] => sample.com/image/4322305/
[8] => sample.com/image/4261476/
[9] => sample.com/image/3386911/
[10] => sample.com/image/4663949/
)
[1] => Array
(
[0] => 4205476
[1] => 4453948
[2] => 427185
[3] => 1323868
[4] => 2505314
[5] => 3300481
[6] => 1046646
[7] => 4322305
[8] => 4261476
[9] => 3386911
[10] => 4663949
)
)
现在您需要记住,PHP将返回它匹配的所有内容,包括正则表达式字符串的不需要的部分。
从PHP手册: http://www.php.net/manual/en/function.preg-match-all.php
对结果进行排序,以便$ matches [0]是一个完整模式的数组 匹配,$ matches [1]是由第一个匹配的字符串数组 带括号的子模式,依此类推。
答案 2 :(得分:0)