Php : 使用 php 脚本替换图片 src 路径

Php : replace image src path using php script

我想创建 php 脚本来更新 img src 路径。

旧结果:

<a href="http://example.com" target="_blank"><img src="{{media url=''}}wysiwyg/images/img_07.jpg" border="1">

预期结果:

<a href="http://example.com" target="_blank"><img src="{{media url='wysiwyg/images/img_07.jpg'}}" border="1">

我使用了下面的代码:

<?php
$servername = "localhost";
$username = "root";
$password = "testing";
$dbname = "database";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT * FROM table_nm WHERE attribute_id = 1";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        echo htmlentities($row["value"]) . "<br>"; //old result text display from here.
    }
} else {
    echo "0 results";
}
$conn->close();
?>

我需要更改什么才能获得预期结果?

您可以试试这个以获得预期的结果

$patterns = ["'}}","jpg","png","jpeg","gif"];
$replacements = ["","jpg'}}","png'}}","jpeg'}}","gif'}}"];
$row["value"] = str_replace($patterns, $replacements, $row["value"]);