PHP - 在无法确定长度时查找并替换两个变量之间的字符串
I'm trying to create a simple PHP
find and replace system by looking at all of the images in the HTML
and add a simple bit of code at the start and end of the image source. The image source has something like this:
<img src="img/image-file.jpg">
and it should become into this:
<img src="{{media url="wysiwyg/image-file.jpg"}}"
The Find
="img/image-file1.jpg"
="img/file-2.png"
="img/image3.jpg"
Replace With
="{{media url="wysiwyg/image-file.jpg"}}"
="{{media url="wysiwyg/file-2.png"}}"
="{{media url="wysiwyg/image3.jpg"}}"
The solution is most likely simple yet from all of the research that I have done. It only works with one string not a variety of unpredictable strings.
Current Progress
$oldMessage = "img/";
$deletedFormat = '{{media url="wysiwyg/';
$str = file_get_contents('Content Slots/Compilied Code.html');
$str = str_replace("$oldMessage", "$deletedFormat",$str);
The bit I'm stuck at is find the "
at the end of the source to add the end of the required code "}}"
我正在尝试通过查看全部创建一个简单的 它应该变成这样: p>
查找 strong> p>
替换为 strong> p >
解决方案很可能很简单而且来自所有的研究 我已经做好了。 它只适用于一个字符串而不是各种不可预测的字符串。 p>
当前进度 strong> p>
我坚持的位置是在结尾处找到 PHP code>查找和替换系统
HTML code>中的图像,并在图像源的开头和结尾添加一些简单的代码。 图像源具有以下内容: p>
&lt; img src =“img / image-file.jpg”&gt;
code> pre>
&lt; img src =“{{media url =”wysiwyg / image-file.jpg“}}” n code> pre>
=“img / image-file1.jpg”\ n =“img / file-2.png”
=“img / image3.jpg”
code> pre>
=“{{media url =”wysiwyg / image-file.jpg“}}”
=“{{media url =”wysiwyg / file-2.png“}} “
=”{{media url =“wysiwyg / image3.jpg”}}“
code> pre>
$ oldMessage = “img /”;
$ deletedFormat ='{{media url =“wysiwyg /';
$ str = file_get_contents('Content Slots / Compilied Code.html');
$ str = str_replace(”$ oldMessage“ ,“$ deletedFormat”,$ str);
code> pre>
“ code> source添加所需代码的结尾
“}}” code> p>
div>
I don't like to build regular expressions to parse HTML
, but it seems that in this case, a regular expression will help you:
$reg = '/=["\']img\/([^"\']*)["\']/';
$src = ['="img/image-file1.jpg"', '="img/file-2.png"', '="img/image3.jpg"'];
foreach ($src as $s) {
$str = preg_replace($reg, '={{media url="wysiwyg/$1"}}', $s);
echo "$str
";
}
Here you have an example on Ideone.
To make it works with your content:
$content = file_get_contents('Content Slots/Compilied Code.html');
$reg = '/=["\']img\/([^"\']*)["\']/';
$final = preg_replace($reg, '={{media url="wysiwyg/$1"}}', $content);
Here you have an example on Ideone.
In my opinion what you are doing is not the best way this can be done. I would use abstract template for this.
<?php
$content = file_get_contents('Content Slots/Compilied Code.html');
preg_match_all('/=\"img\/(.*?)\"/', $content, $matches);
$finds = $matches[1];
$abstract = '="{{media url="wysiwyg/{filename}"}}"';
$concretes = [];
foreach ($finds as $find) {
$concretes[] = str_replace("{filename}", $find, $abstract);
}
// $conretes[] will now have all matches formed properly...
Edit:
To return full html use this:
<?php
$content = file_get_contents('Content Slots/Compilied Code.html');
preg_match_all('/=\"img\/(.*)\"/', $content, $matches);
$finds = $matches[1];
$abstract = '="{{media url="wysiwyg/{filename}"}}"';
foreach ($finds as $find) {
$content = preg_replace('/=\"img\/(.*)\"/', str_replace("{filename}", $find, $abstract), $content, 1);
}
echo $content;