doureng5668 2018-04-03 11:46
浏览 81

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

Okay i've got this working with the $_POST["HTMLSourceCode"] like following:

Text area:

<!-- 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:

// 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); 

However the problem i'm having is when the $_POST["HTMLSourceCode"] is turned into a string then the preg_match_all() turns back an empty $result array

Not working example:

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

starting tag:

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

ending tag:

<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;

Result:

Array ( [0] => Array ( ) )

Expected result: (Based on working example)

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条回答 默认 最新

  • dovzrfr0506 2018-04-03 12:30
    关注

    I found the answer for the reason why the string wasn't working once I did the imploding.

    I had to change:

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

    to

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

    I had to use html_entity_decode() which here is some documentation on the usage: link

    评论

报告相同问题?

悬赏问题

  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64