I want to replace the image links in the text with the following, but it doesn't. I'm sure everything is correct but I don't know what the problem is. Can you help me with this issue?
{#img='image_name.jpg', alt=''}
PHP code:
$text = "Hello <img src='https://example.com/image1.jpg'> , <img src='example.com/image2.jpg'> and now you :)";
$re = '/<img.*?src=[\'"]([^"\'])[\'"]>/';
preg_match_all($re, $text, $matches, PREG_SET_ORDER, 0);
foreach ($matches as $val) {
$str = preg_replace('/<img.*?src=[\'"](' . $val[1] . ')[\'"]>/', "{#img='" . $val[1] . "', alt=''}", $text);
}
var_dump($text);
Result:
Hello <img src='https://example.com/image1.jpg'> , <img src='https://example.com/image2.jpg'> and now you :)
My Answer:
Sure, I can help you with that. Can you please provide me with the code you are using to replace the image links in the text? This will help me better understand the issue and provide you with a solution.
Rate this post
5 of 5 based on 6221 votesComments