使用preg_match_all()查找所有图像标签

时间:2018-04-03 11:46:35

标签: php preg-match-all

好的,我已经使用了$_POST["HTMLSourceCode"],如下所示:

文字区域:

<!-- Text area -->
<div class="form-group sourceCode resourceBox">
   <textarea class="form-control" name="HTMLSourceCode" id="HTMLSourceCode" rows="3" placeholder="Source code"></textarea>
</div>

源代码:

// Source code which is posted within textarea
<div id="element_36866219" class="element element-slideshow" >
<div class="sj_element_slideshow "><h2 class="custom_title"></h2><div class="sj-block-center" style="position: relative; max-width:100%; height:0; padding-bottom:60%;"><div  id="sj_element_slideshow_36866219" class="cycle" style="position: absolute; width: 100%; height: 100%;"><div style='position: absolute; top: 0; left: 0;'><img class="lazy" style="width: auto; height: auto; max-width: 100%; max-height: 100%;" data-outline="" data-host="https://img.cdn.schooljotter2.com/sampled/" data-width="100" data-height="60" data-id="10305401" data-original="https://img.cdn.schooljotter2.com/sampled/10305401/1200/720" /></div>
<div style='position: absolute; top: 0; left: 0;'><img class="lazy" style="width: auto; height: auto; max-width: 100%; max-height: 100%;" data-outline="" data-host="https://img.cdn.schooljotter2.com/sampled/" data-width="100" data-height="60" data-id="10305446" data-original="https://img.cdn.schooljotter2.com/sampled/10305446/1200/720" /></div>
<div style='position: absolute; top: 0; left: 0;'><img class="lazy" style="width: auto; height: auto; max-width: 100%; max-height: 100%;" data-outline="" data-host="https://img.cdn.schooljotter2.com/sampled/" data-width="100" data-height="60" data-id="10305449" data-original="https://img.cdn.schooljotter2.com/sampled/10305449/1200/720" /></div>
<div style='position: absolute; top: 0; left: 0;'><img class="lazy" style="width: auto; height: auto; max-width: 100%; max-height: 100%;" data-outline="" data-host="https://img.cdn.schooljotter2.com/sampled/" data-width="100" data-height="60" data-id="10305452" data-original="https://img.cdn.schooljotter2.com/sampled/10305452/1200/720" /></div>
<div style='position: absolute; top: 0; left: 0;'><img class="lazy" style="width: auto; height: auto; max-width: 100%; max-height: 100%;" data-outline="" data-host="https://img.cdn.schooljotter2.com/sampled/" data-width="100" data-height="60" data-id="10305455" data-original="https://img.cdn.schooljotter2.com/sampled/10305455/1200/720" /></div>
<div style='position: absolute; top: 0; left: 0;'><img class="lazy" style="width: auto; height: auto; max-width: 100%; max-height: 100%;" data-outline="" data-host="https://img.cdn.schooljotter2.com/sampled/" data-width="100" data-height="60" data-id="10305458" data-original="https://img.cdn.schooljotter2.com/sampled/10305458/1200/720" /></div>
<div style='position: absolute; top: 0; left: 0;'><img class="lazy" style="width: auto; height: auto; max-width: 100%; max-height: 100%;" data-outline="" data-host="https://img.cdn.schooljotter2.com/sampled/" data-width="100" data-height="60" data-id="10305461" data-original="https://img.cdn.schooljotter2.com/sampled/10305461/1200/720" /></div>
<div style='position: absolute; top: 0; left: 0;'><img class="lazy" style="width: auto; height: auto; max-width: 100%; max-height: 100%;" data-outline="" data-host="https://img.cdn.schooljotter2.com/sampled/" data-width="100" data-height="60" data-id="10305464" data-original="https://img.cdn.schooljotter2.com/sampled/10305464/1200/720" /></div>
<div style='position: absolute; top: 0; left: 0;'><img class="lazy" style="width: auto; height: auto; max-width: 100%; max-height: 100%;" data-outline="" data-host="https://img.cdn.schooljotter2.com/sampled/" data-width="100" data-height="60" data-id="10305466" data-original="https://img.cdn.schooljotter2.com/sampled/10305466/1200/720" /></div>
<div style='position: absolute; top: 0; left: 0;'><img class="lazy" style="width: auto; height: auto; max-width: 100%; max-height: 100%;" data-outline="" data-host="https://img.cdn.schooljotter2.com/sampled/" data-width="100" data-height="60" data-id="10305469" data-original="https://img.cdn.schooljotter2.com/sampled/10305469/1200/720" /></div>
<div style='position: absolute; top: 0; left: 0;'><img class="lazy" style="width: auto; height: auto; max-width: 100%; max-height: 100%;" data-outline="" data-host="https://img.cdn.schooljotter2.com/sampled/" data-width="100" data-height="60" data-id="10305472" data-original="https://img.cdn.schooljotter2.com/sampled/10305472/1200/720" /></div>
<div style='position: absolute; top: 0; left: 0;'><img class="lazy" style="width: auto; height: auto; max-width: 100%; max-height: 100%;" data-outline="" data-host="https://img.cdn.schooljotter2.com/sampled/" data-width="100" data-height="60" data-id="10305478" data-original="https://img.cdn.schooljotter2.com/sampled/10305478/1200/720" /></div>
<div style='position: absolute; top: 0; left: 0;'><img class="lazy" style="width: auto; height: auto; max-width: 100%; max-height: 100%;" data-outline="" data-host="https://img.cdn.schooljotter2.com/sampled/" data-width="100" data-height="60" data-id="10305482" data-original="https://img.cdn.schooljotter2.com/sampled/10305482/1200/720" /></div>
<div style='position: absolute; top: 0; left: 0;'><img class="lazy" style="width: auto; height: auto; max-width: 100%; max-height: 100%;" data-outline="" data-host="https://img.cdn.schooljotter2.com/sampled/" data-width="100" data-height="60" data-id="10305485" data-original="https://img.cdn.schooljotter2.com/sampled/10305485/1200/720" /></div>
<div style='position: absolute; top: 0; left: 0;'><img class="lazy" style="width: auto; height: auto; max-width: 100%; max-height: 100%;" data-outline="" data-host="https://img.cdn.schooljotter2.com/sampled/" data-width="100" data-height="60" data-id="10305488" data-original="https://img.cdn.schooljotter2.com/sampled/10305488/1200/720" /></div>
<div style='position: absolute; top: 0; left: 0;'><img class="lazy" style="width: auto; height: auto; max-width: 100%; max-height: 100%;" data-outline="" data-host="https://img.cdn.schooljotter2.com/sampled/" data-width="100" data-height="60" data-id="10305493" data-original="https://img.cdn.schooljotter2.com/sampled/10305493/1200/720" /></div>
<div style='position: absolute; top: 0; left: 0;'><img class="lazy" style="width: auto; height: auto; max-width: 100%; max-height: 100%;" data-outline="" data-host="https://img.cdn.schooljotter2.com/sampled/" data-width="100" data-height="60" data-id="10305495" data-original="https://img.cdn.schooljotter2.com/sampled/10305495/1200/720" /></div>
</div></div></div>
<div id="element_36866220" class="element element-text" >

preg_match_all()

// This is from textarea
preg_match_all('/<img[^>]+>/i', $_POST["HTMLSourceCode"], $result); 

然而我遇到的问题是当$_POST["HTMLSourceCode"]变成string时,preg_match_all()会转回一个空的$result数组

不工作的例子:

<div class="form-group col-xs-12 col-sm-12 col-md-6 col-lg-6 selectorTags left">
    <input type="text" class="form-control" name="startingTag" id="startingTag" placeholder="Starting Tag">
</div>
<div class="form-group col-xs-12 col-sm-12 col-md-6 col-lg-6 selectorTags right">
    <input type="text" class="form-control" name="endingTag" id="endingTag" placeholder="Ending Tag">
</div>
<div class="form-group sourceCode resourceBox">
   <textarea class="form-control" name="HTMLSourceCode" id="HTMLSourceCode" rows="3" placeholder="Source code"></textarea>
</div>

开始标记:

<div id="element_36866219" class="element element-slideshow" >

结束标记:

<div id="element_36866220" class="element element-text" >

$HTMLSourceCode = "";

if(isset($_POST["startingTag"]) && !empty($_POST["startingTag"]) && isset($_POST["endingTag"]) && !empty($_POST["endingTag"])){
    $tagArrayTemp = array();
    $tagArrayTemp = array_values(array_filter(explode(htmlentities($_POST["startingTag"]), htmlentities($_POST["HTMLSourceCode"]))));
    $tagArray = array_values(array_filter(explode(htmlentities($_POST["endingTag"]), $tagArrayTemp[0])));

    if($tagArray[0]){
        $HTMLSourceCode = $tagArray[0];
    } else {
        $HTMLSourceCode = $_POST["HTMLSourceCode"];
    }
} else {
    $HTMLSourceCode = $_POST["HTMLSourceCode"];
}

if(!empty($_POST["HTMLSourceCode"])){
    preg_match_all('/<img[^>]+>/i', $HTMLSourceCode, $result); 
} 

print_r($result);
exit;

结果:

Array ( [0] => Array ( ) )

预期结果:(基于工作示例)

Array ( [0] => Array ( [0] =>  [1] =>  [2] =>  [3] =>  [4] =>  [5] =>  [6] =>  [7] =>  [8] =>  [9] =>  [10] =>  [11] =>  [12] =>  [13] =>  [14] =>  [15] =>  [16] =>  [17] =>  [18] =>  [19] =>  [20] =>  [21] =>  [22] =>  [23] =>  [24] =>  [25] =>  [26] =>  [27] =>  [28] =>  ) )

1 个答案:

答案 0 :(得分:0)

我找到答案的原因是,一旦我完成了爆炸,字符串就不起作用了。

我不得不改变:

if($tagArray[0]){
    $HTMLSourceCode = $tagArray[0];
} else {
    $HTMLSourceCode = $_POST["HTMLSourceCode"];
}

if($tagArray[0]){
    $HTMLSourceCode = html_entity_decode($tagArray[0]);
} else {
    $HTMLSourceCode = $_POST["HTMLSourceCode"];
}

我必须使用html_entity_decode(),这里有一些关于用法的文档:link