Page 1 of 1

Preg_replace() Delimiter problem

Posted: Thu Apr 19, 2018 9:02 am
by geralde.givens
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: Thu Apr 19, 2018 2:17 pm
by protopatterns
Try this:

Code: Select all

$pattern = '#http://#';
See here for why: http://php.net/manual/en/regexp.referen ... miters.php

Re: Preg_replace() Delimiter problem

Posted: Thu Apr 19, 2018 2:37 pm
by Christopher
Or escape the slashes:

Code: Select all

$pattern = '/http:\/\//';
PS - for the preg_ functions you need to have delimiters in the regex, so 'http://' would not have worked anyway.