Php 查找以“开始”开头的字符串;X";并以“结束”;“是”;并在之间替换内容

Php 查找以“开始”开头的字符串;X";并以“结束”;“是”;并在之间替换内容,php,regex,Php,Regex,我正在尝试处理一些html并用base64替换所有img标记src。我已经编写了下面的函数来转换图像并以base64返回它。我需要以下方面的帮助: 我需要使用str_replace、preg_replace或某种排序正则表达式来扫描一些html,并用图像的base64表示替换所有的“src”。html存储为变量,而不是实际的html文档。例如,如果我有一些html,如: $htmlSample = "<div>Some text, yada yada and now and ima

我正在尝试处理一些html并用base64替换所有img标记src。我已经编写了下面的函数来转换图像并以base64返回它。我需要以下方面的帮助:


我需要使用str_replace、preg_replace或某种排序正则表达式来扫描一些html,并用图像的base64表示替换所有的“src”。html存储为变量,而不是实际的html文档。例如,如果我有一些html,如:

$htmlSample =  "<div>Some text, yada yada and now and image <img src='image1.png' /></div>"
$htmlSample=“一些文本、yada-yada和now-and-image”
我需要扫描它并用base64等价物替换src='image.png',类似于src=“data:image/png;base64,/9j/4wvurxhpzgaasukaaaaaaaaaaaia8bagaaababagaaabagababagak”(这不是实际的base64,只是一些填充文本)。该函数需要能够对html中的多个图像执行此操作。如果你能给我指出正确的方向,我将非常感激。谢谢大家

function convertImage($file)
{


    if($fp = fopen($file,"rb", 0))
    {
       $picture = fread($fp,filesize($file));
       fclose($fp);
       $base64 = base64_encode($picture);
       $tag = '<img ' . "" .
          'src="data:image/png;base64,' . $base64 .
          '"  />';
       return $tag;
    }

}
函数convertImage($file)
{
如果($fp=fopen($file,“rb”,0))
{
$picture=fread($fp,filesize($file));
fclose($fp);
$base64=base64_编码($picture);
$tag='';
返回$tag;
}
}

看看像SimpleDOM这样的DOM操纵器。这将使您能够以更面向对象的方式解析html文档,而不是凌乱的正则表达式,因为这些库更有可能处理您可能想不到的情况

正如Adam所建议的,我能够使用SimpleDOM(链接:simplehtmldom.sourceforge.net)完成这项工作

require_once('simple_html_dom.php');
$html=“这是一些测试代码,这是一些附加文本和图像:”;
//使用simple_html_dom.php中的函数使html可解析
$doc=str_get_html($html);
//在html中查找每个图像并进行转换
foreach($doc->find($img[src]”)作为$img)
{
//获取图像的src并分配给$src
$src=$img->src;
$imageBase=convertImage($src);
$img->src=$imageBase;
}
$html=(字符串)$doc;
echo$html;
函数convertImage($file)
{
//根据上面的$src名称查找文件,如果文件存在,则运行代码
如果($fp=fopen($file,“rb”,0))
{
$picture=fread($fp,filesize($file));
fclose($fp);
//将图像文件转换为base64
$base64=base64_编码($picture);
//将必需数据:+base64代码返回到上面的$imageBase,以插入html>img>src
返回'data:image/png;base64'。$base64;
}
}

正则表达式不是合适的工具。刚说.,.可能与日期模因相反的重复,正则表达式对于简单的字符串搜索就足够了。这个任务已经被讨论过很多次了,它不是一个实际的文件,它是一个带有文本的字符串,我需要找到一些文本并在运行函数后替换它。字符串是否包含html真的很重要吗?html存储为变量,而不是实际的html文档。SimpleDOM还会使用类似的东西吗?是的,SimpleDOM有多种方法可以通过变量、URL或文件加载页面。我刚找到链接:。文件也很简单。
require_once('simple_html_dom.php');
$html = "This is some test code <img width='50' src='img/paddock1.jpg' /> And this is some additional text and an image: <img src='img/paddock2.jpg' />";

//uses function from simple_html_dom.php to make html parsable
$doc = str_get_html($html);

//finds each image in html and converts
foreach ($doc->find('img[src]') as $img) 
{

    //get src of image and assign to $src
    $src = $img->src;

    $imageBase = convertImage($src);

    $img->src = $imageBase;


}

$html = (string) $doc;

echo $html;

function convertImage($file)
{

    //finds file based on $src name from above and runs code if file exists
    if($fp = fopen($file,"rb", 0))
    {
       $picture = fread($fp,filesize($file));
       fclose($fp);
       //converts image file to base64
        $base64 = base64_encode($picture);

       //returns nessary data: + base64 code to $imageBase above to be inserted into html>img>src
       return 'data:image/png;base64,' . $base64;
    }
}