0

我正在尝试从 html 内容中提取一些特定的图片。目前我有以下数组

   [1] => 数组
        (
            [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~~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~~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~~
        )

我只需要提取以 ! 并以 12.JPG 结尾。预期结果的一个例子是 !BuEZm5g!Wk~$(KGrHqQH-DgEvrb2CLuOBL-vbkHlKw~~_12.JPG 。我有一个模式,但由于某些我不知道的原因它不起作用。它是

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

预先感谢您的任何帮助

4

2 回答 2

2

不是正则表达式问题。

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

foreach ($names as $name)
{
    if ((substr($name, 0, 1) == '!') && (substr($name, -6) == '12.JPG'))
    {
        $keep[] = $name;
    }
}
于 2010-06-05T19:36:22.770 回答
1

尝试'/#\!([^_]+)_12.JPG/'改用。另外,您正在使用preg_match_all,对吗?

例子 ..

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

输出(注意如何$matches[0]包含所有完整匹配,并$matches[1]包含所有分组(最多 _12.JPG)匹配)..

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!~~"
  }
}
于 2010-06-05T19:38:33.467 回答