搜索整个文本中的图像

问题描述 投票:0回答:1

我的项目遇到问题。 我需要在字符串中搜索图像。 我想获取图像的来源并修改img标签的html形式。 例如图像形式为:

我想将其更改为:

<div class="col-md-3">
<hr class="visible-sm visible-xs tall" />
<a class="img-thumbnail lightbox pull-left" href="upload/uploader/up_164.jpg" data-plugin-options='{"type":"image"}' title="Image title">
<img class="img-responsive" width="215" src="upload/uploader/up_164.jpg"><span class="zoom"><i class="fa fa-search"></i>
</span></a>

我已经完成了其中的一部分。

我可以找到图像,更改 html 的形式,但无法对字符串中找到的所有图像循环此操作。

我的代码如下

使用以下函数我获取两个字符串之间的字符串

//  Get substring between
function GetBetween($var1="",$var2="",$pool){
    $temp1 = strpos($pool,$var1)+strlen($var1);
    $result = substr($pool,$temp1,strlen($pool));
    $dd=strpos($result,$var2);
    if($dd == 0){
    $dd = strlen($result);
    }

    return substr($result,0,$dd);
}

然后我从字符串中获取图像标签

$imageFile = GetBetween("img","/>",$newText);

接下来是过滤图像来源:

$imageSource = GetBetween('src="','\"',$imageFile);

对于最后一部分,我调用 str_replace 来完成这项工作:

$newText = str_replace('oldform', 'newform', $newText);

问题是,如果有多个图像,我无法循环此过程。

提前谢谢您。

php string search
1个回答
0
投票

读取 xml 文件的最佳、简单且安全的方法是使用 xml 解析器。

而且,我认为你会赢得很多时间。

© www.soinside.com 2019 - 2024. All rights reserved.