0

I want to create php script to update img src path.

old result :

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

expected result :

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

I used this below code :

<?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();
?>

What should i need to change to get expected result?

2
  • What exactly is in here echo htmlentities($row["value"]) . "<br>";? Can you show us? How do you display that in img src? Commented Apr 29, 2019 at 11:29
  • @BudimirSkrtic old result is htmlentities($row["value"]); Commented Apr 29, 2019 at 11:31

1 Answer 1

1

You can try this to get the expected result

$patterns = ["'}}","jpg","png","jpeg","gif"];
$replacements = ["","jpg'}}","png'}}","jpeg'}}","gif'}}"];
$row["value"] = str_replace($patterns, $replacements, $row["value"]);
Sign up to request clarification or add additional context in comments.

2 Comments

Your code remove img extensions. I mean it remove jpg also.
Can you please also add like jpg,png,gif,jpeg this also working ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.