views:

115

answers:

2

I'm trying to extract some specific pictures from html content . Currently I have the following array

   [1] => Array
        (
            [0] => BuEZm5g!Wk~$(KGrHqQH-DgEvrb2CLuOBL-vbkHlKw~~_14.JPG#!BuEZm5g!Wk~$(KGrHqQH-DgEvrb2CLuOBL-vbkHlKw~~_12.JPG|1#http://i.ebayimg.com/08#!BuEZqMQBGk~$(KGrHqQH-DoEvrKYSoPiBL-vb)WgLw~~_14.JPG#!BuEZqMQBGk~$(KGrHqQH-DoEvrKYSoPiBL-vb)WgLw~~_12.JPG|2#http://i.ebayimg.com/13#!BuEZtkw!2k~$(KGrHqYH-EYEvsh4EjJSBL-vb-bLow~~_14.JPG#!BuEZtkw!2k~$(KGrHqYH-EYEvsh4EjJSBL-vb-bLow~~_12.JPG|3#http://i.ebayimg.com/03#!BuEZ)!wEGk~$(KGrHqYH-DwEvq8Z1JuQBL-vcMOoFQ~~_14.JPG#!BuEZ)!wEGk~$(KGrHqYH-DwEvq8Z1JuQBL-vcMOoFQ~~_12.JPG|4#http://i.ebayimg.com/09#!BuEZ1IQEGk~$(KGrHqEH-D0EvqwClvviBL-vcclJwg~~_14.JPG#!BuEZ1IQEGk~$(KGrHqEH-D0EvqwClvviBL-vcclJwg~~_12.JPG|5#http://i.ebayimg.com/17#!BuEZ4FQCWk~$(KGrHqUH-DMEvS+,FRj5BL-vco)Qgg~~_14.JPG#!BuEZ4FQCWk~$(KGrHqUH-DMEvS+,FRj5BL-vco)Qgg~~_12.JPG|6#http://i.ebayimg.com/17#!BuEZ7FQEWk~$(KGrHqYH-EYEvsh4EjJSBL-vc1v2Hg~~_14.JPG#!BuEZ7FQEWk~$(KGrHqYH-EYEvsh4EjJSBL-vc1v2Hg~~_12.JPG|7#http://i.ebayimg.com/12#!BuEZ-c!Bmk~$(KGrHqQH-CYEvr5z9)NVBL-vdC,)Mg~~_14.JPG#!BuEZ-c!Bmk~$(KGrHqQH-CYEvr5z9)NVBL-vdC,)Mg~~_12.JPG|8#http://i.ebayimg.com/20#!BuE,CNgCWk~$(KGrHqIH-CIEvqKBurmhBL-vdRBe3!~~_14.JPG#!BuE,CNgCWk~$(KGrHqIH-CIEvqKBurmhBL-vdRBe3!~~_12.JPG|9#http://i.ebayimg.com/24#!BuE,FN!EWk~$(KGrHqUH-C0EvsBdjbv0BL-vdeFkD!~~_14.JPG#!BuE,FN!EWk~$(KGrHqUH-C0EvsBdjbv0BL-vdeFkD!~~_12.JPG|10#http://i.ebayimg.com/16#!BuE,IOgBmk~$(KGrHqEH-EMEvpym7mSLBL-vdqI6nw~~_14.JPG#!BuE,IOgBmk~$(KGrHqEH-EMEvpym7mSLBL-vdqI6nw~~
        )

I need to extract only the pictures that start with ! and ends in 12.JPG . An example of expected result is !BuEZm5g!Wk~$(KGrHqQH-DgEvrb2CLuOBL-vbkHlKw~~_12.JPG . I have a pattern but it doesn't work for some reasons that I don't know . It is

$pattern = "/#\!(.*)_12.JPG/";

Thank you in advance for any help

+2  A: 

Not a regex problem.

$names = explode('#', $names);
$keep = array();

foreach ($names as $name)
{
    if ((substr($name, 0, 1) == '!') && (substr($name, -6) == '12.JPG'))
    {
        $keep[] = $name;
    }
}
Coronatus
+1  A: 

Try using '/#\!([^_]+)_12.JPG/' instead. Also, you're using preg_match_all, right?

Example ..

<?php
$input = '(that long string you have)';
$matches = array();
preg_match_all('/#\!([^_]+)_12.JPG/', $input, $matches);
var_dump($matches);

Output (notice how $matches[0] contains all full matches, and $matches[1] contains all grouped (up to _12.JPG) matches) ..

array(2) {
  [0]=>
  array(10) {
    [0]=>
    string(53) "#!BuEZm5g!Wk~$(KGrHqQH-DgEvrb2CLuOBL-vbkHlKw~~_12.JPG"
    [1]=>
    string(53) "#!BuEZqMQBGk~$(KGrHqQH-DoEvrKYSoPiBL-vb)WgLw~~_12.JPG"
    [2]=>
    string(53) "#!BuEZtkw!2k~$(KGrHqYH-EYEvsh4EjJSBL-vb-bLow~~_12.JPG"
    [3]=>
    string(53) "#!BuEZ)!wEGk~$(KGrHqYH-DwEvq8Z1JuQBL-vcMOoFQ~~_12.JPG"
    [4]=>
    string(53) "#!BuEZ1IQEGk~$(KGrHqEH-D0EvqwClvviBL-vcclJwg~~_12.JPG"
    [5]=>
    string(53) "#!BuEZ4FQCWk~$(KGrHqUH-DMEvS+,FRj5BL-vco)Qgg~~_12.JPG"
    [6]=>
    string(53) "#!BuEZ7FQEWk~$(KGrHqYH-EYEvsh4EjJSBL-vc1v2Hg~~_12.JPG"
    [7]=>
    string(53) "#!BuEZ-c!Bmk~$(KGrHqQH-CYEvr5z9)NVBL-vdC,)Mg~~_12.JPG"
    [8]=>
    string(53) "#!BuE,CNgCWk~$(KGrHqIH-CIEvqKBurmhBL-vdRBe3!~~_12.JPG"
    [9]=>
    string(53) "#!BuE,FN!EWk~$(KGrHqUH-C0EvsBdjbv0BL-vdeFkD!~~_12.JPG"
  }
  [1]=>
  array(10) {
    [0]=>
    string(44) "BuEZm5g!Wk~$(KGrHqQH-DgEvrb2CLuOBL-vbkHlKw~~"
    [1]=>
    string(44) "BuEZqMQBGk~$(KGrHqQH-DoEvrKYSoPiBL-vb)WgLw~~"
    [2]=>
    string(44) "BuEZtkw!2k~$(KGrHqYH-EYEvsh4EjJSBL-vb-bLow~~"
    [3]=>
    string(44) "BuEZ)!wEGk~$(KGrHqYH-DwEvq8Z1JuQBL-vcMOoFQ~~"
    [4]=>
    string(44) "BuEZ1IQEGk~$(KGrHqEH-D0EvqwClvviBL-vcclJwg~~"
    [5]=>
    string(44) "BuEZ4FQCWk~$(KGrHqUH-DMEvS+,FRj5BL-vco)Qgg~~"
    [6]=>
    string(44) "BuEZ7FQEWk~$(KGrHqYH-EYEvsh4EjJSBL-vc1v2Hg~~"
    [7]=>
    string(44) "BuEZ-c!Bmk~$(KGrHqQH-CYEvr5z9)NVBL-vdC,)Mg~~"
    [8]=>
    string(44) "BuE,CNgCWk~$(KGrHqIH-CIEvqKBurmhBL-vdRBe3!~~"
    [9]=>
    string(44) "BuE,FN!EWk~$(KGrHqUH-C0EvsBdjbv0BL-vdeFkD!~~"
  }
}
Matt