Replacing code in regular expressions

The problem is that links on the site are shortened, like index.php. That’s why the links don’t work on my site. How can I replace href=”ssilka.php” with href=”http://www.sait.ru/ssilka.php”. Well, or somehow create a ssilka.php page on my site that would redirect to that site and save the _GET method in the link.


Answer 1, authority 100%

How can I replace href=”ssilka.php”
on the
href=”http://www.sait.ru/ssilka.php”.

you can replace it like this:

$pref = "http://www.sait.ru/";
$page = '...    href="ssilka.php"  href="ssilka2.php".';
$page = preg_replace('#href="([a-zA-Z0-9.]+)"#i', 'href="'.$pref.'$1"', $page);
echo $page;

Will output: … How can I replace href=”http://www.sait.ru/ssilka.php” with href=”http:// www.sait.ru/ssilka2.php“.


Answer 2, authority 100%

If I understand correctly, then something like this

$pattern = '/href="([^"]*)"/i';
$replacement = '/href="URL/${1}"';
preg_replace($pattern, $replacement, $YOUR_STRING);

Replaces all links in the text.