I'm making a html template that requires reletive links but for templeting i must use php (very noob), in the index file i'd like to remove all ../
from href an src, like this exemple:
page.php
scr="../img/image.jpg"
href="../contact/contact.php"
index.php
scr="img/image.jpg"
href="contact/contact.php"
How can I create a function to remove all ../
Thanks
I read the template in using get_file_contents then replace the string:
$sContents = file_get_contents('<URL TO FILE>');
$sContents = str_replace('../', '', $sContents);
If it's really so that the ../
is present just once at the beginning the following will work:
echo str_replace('../', $string);
if it could be that there is a presence of ../
in the middle of the path which should not being removed use preg_replace()
:
$str = '../img/image.jpg';
echo preg_replace('~^(\.\./)(.*)$~', "$2", $str);