[英]preg_match_all for this pattern
i have this pattern and i ant to use it to extract the numbers after the /image/ field and i have tried this pattern and i have checked online at http://www.functions-online.com/preg_match_all.html and it is giving desired output for the first link but for other links it is not giving desired output here is my pattern 我有这种模式,我用它来提取/图像/字段后的数字,我已经尝试过这种模式,我已经在线检查了http://www.functions-online.com/preg_match_all.html它是为第一个链接提供所需的输出但是对于其他链接,它没有给出所需的输出,这是我的模式
/\sample.com\/image\/(.*)\//
and here is my string 这是我的字符串
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]
eg 例如
i want to extract only number after /image/ field i dont want image name my desired output is 我想在/ image / field之后只提取数字我不想要图像名称我想要的输出
4205476
4453948
427185
etc all numbers from string 等所有来自字符串的数字
Use this Regular Expression ~\\/\\image\\/(.*?)\\/~
使用此正则表达式~\\/\\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
)
You need to adjust your regular expression: 您需要调整正则表达式:
$regex = '@sample\.com/image/([0-9]+)/@'
preg_match_all('@sample\.com/image/([0-9]+)/@', $str, $m);
print_r($m);
Expected output: 预期产量:
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
)
)
Now you'll need to keep in mind that PHP will return everything it matches including the undesired parts of the regex string. 现在你需要记住PHP将返回它匹配的所有内容,包括正则表达式字符串的不需要的部分。
From the PHP Manual: http://www.php.net/manual/en/function.preg-match-all.php 从PHP手册: http : //www.php.net/manual/en/function.preg-match-all.php
Orders results so that $matches[0] is an array of full pattern matches, $matches[1] is an array of strings matched by the first parenthesized subpattern, and so on. 对结果进行排序,以便$ matches [0]是完整模式匹配的数组,$ matches [1]是由第一个带括号的子模式匹配的字符串数组,依此类推。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.