A PHP function that uses regular expressions to match image links in an article and replace them with specified image links:
function replaceImageLinks($content, $newImageLink) {
// 匹配 img 标签中的 src 属性
$pattern = '/<img.*?src="(.*?)".*?>/i';
// 用指定链接替换匹配到的图片链接
$replacement = '<img src="' . $newImageLink . '">';
// 替换
$newContent = preg_replace($pattern, $replacement, $content);
return $newContent;
}This function accepts two parameters: the original article content and the new image link to be used as a replacement.
The function uses regular expressions to match image links in the article and uses preg_replace() The function replaces the matched image link with the specified new image link. Finally, the function returns the newly replaced content.
You can call this function in the following ways:
$content = '这是一篇文章,其中包含图片:<img src="https://example.com/image.jpg">。';
$newImageLink = 'https://example.com/new-image.jpg';
$newContent = replaceImageLinks($content, $newImageLink);
echo $newContent;among,$content This is the original article content,$newImageLink This is the new image link that will replace the existing ones. After calling the function, the function replaces all image links in the article with the specified new image link and returns the newly replaced content.
Secondary demand
An article of mine contains many images, and I want to replace the link for each image, but I don't want all the new links to be the same!
Therefore, the second parameter should be changed to an array containing links to dozens of randomly selected images, which will then be randomly replaced!
At the same time, the images on some websites may havestyleThere are all kinds of annotations; sometimes I just want to replace the corresponding image links, while at other times I want to replace all img tags with a clean version (for example, <img src="https://www.4s5.cn/1.jpg" />)
So the function has been modified!
This function accepts two parameters: the original article content and an array containing multiple image links. $imageLinks。
The function uses regular expressions to match image links within the article, and then from $imageLinks Randomly select one image link from the array, then replace all matching image links with that link. Finally, the function returns the newly replaced content.
You can call this function in the following ways:
$content = '这是一篇文章,其中包含多张图片:<img src="https://example.com/image1.jpg">,<img src="https://example.com/image2.jpg">,<img src="https://example.com/image3.jpg">。';
$imageLinks = ['https://example.com/new-image1.jpg', 'https://example.com/new-image2.jpg', 'https://example.com/new-image3.jpg'];
$newContent = randomReplaceImageLinks($content, $imageLinks,0);
echo $newContent;among,$content This is the original article content,$imageLinks It is an array containing multiple image links. After the function is called, the function will randomly select $imageLinks Given a single image link within an array, replace all image links in the article with that link and return the newly replaced content.