I have a string coming from database as given below. I want to change the img src in the text below to be ./upload-files/<filename>
instead of ../upload-files/<filename>
.
<p><span style="text-decoration: line-through;">
<img style="float: left; margin-left: 5px; margin-right: 5px;" src="../upload-files/teddybear.jpg">
</span></p>
So basically I want image to use the current directory instead of the parent directory ../
How can i do this? Is preg_replace the right tool here?
If you are using an intermediary variable to store your image you can use:
$img_url = '../upload-files/teddybear.jpg';
$img_url = print_r(preg_replace('/^\.\./', '.', $img_url));
<p><span style="text-decoration: line-through;">
<img style="float: left; margin-left: 5px; margin-right: 5px;" src="<?php echo $img_url;?>">
</span></p>
if you are trying to replace ../
inside a bunch of html, use this:
$my_html = '<p><span style="text-decoration: line-through;">
<img style="float: left; margin-left: 5px; margin-right: 5px;" src="../upload-files/teddybear.jpg">
</span></p>';
print_r(preg_replace('/"\.\./', '".', $my_html));