سؤال

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

  /\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

     4205476
     4453948
      427185

etc all numbers from string

هل كانت مفيدة؟

المحلول

Use this Regular Expression ~\/\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
)

enter image description here

نصائح أخرى

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.

From the PHP Manual: 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.

Try this:

/.*sample\.com\/image\/(\d+)\/.*/

Regular expression visualization

Debuggex Demo

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top