Page 1 of 1

Preg_replace() Delimiter problem

Posted: Sat May 27, 2017 6:47 pm
by UniqueIdeaMan
Guys,

What I am trying to do is load google.com with cURL and no matter what link is on the page, be it a google link or another domain), I want the script to precede: http://mydomain.com/tracker.php?

So, if the page contains a link like this:

http://somedomain.com/

then that should be replaced with:

http://mydomain.com/tracker.php?http://somedomain.com/

And, if the page contains link like this:

http://subdomain.somedomain.com/dir/sub-dir/page.html

Then, I want it to be replaced to:

http://mydomain.com/tracker.php?http:// ... /page.html

You get my point ?

Here's my code but I get error:

Warning: preg_replace(): Delimiter must not be alphanumeric or backslash in C:\xampp\htdocs\test\CURL_experiment1.php on line 13

Here's my code:

Code: Select all

<?php
$url = "http://google.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "$url");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
curl_close($ch);

$pattern = 'http://';
$replacement = 'http://mydomain.com/tracker.php?';
echo preg_replace($pattern, $replacement, $result);

?>

Re: Preg_replace() Delimiter problem

Posted: Sat May 27, 2017 6:48 pm
by UniqueIdeaMan
Tried the following but still not working:

Code: Select all

<?php
$url = "http://google.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "$url");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
curl_close($ch);

str_replace("https://","http://mydomain.com/tracker.php?https://","$result");
str_replace("http://","http://mydomain.com/tracker.php?http://","$result");
str_replace("localhost","http://mydomain.com/tracker.php?$url","$result");
echo $result;

?>

In short, all links on google homepage should be preceded by:
http://mydomain.com/tracker.php?

Any links containing "localhost" should be replaced with:
$url.
And, that $url should be preceded by:
http://mydomain.com/tracker.php?

So, a link that looks like this:
localhost/contactus.html
should be replaced by:
http://mydomain.com/tracker.php?http:// ... actus.html