PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Thu Oct 18, 2018 12:06 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Thu Apr 19, 2018 9:02 am 
Offline
Forum Newbie

Joined: Tue Apr 10, 2018 12:21 pm
Posts: 2
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:
Syntax: [ Download ] [ Hide ]
<?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);

?>


Top
 Profile  
 
PostPosted: Thu Apr 19, 2018 2:17 pm 
Offline
Forum Newbie

Joined: Sun Jan 28, 2018 12:18 pm
Posts: 9
Try this:

Syntax: [ Download ] [ Hide ]
  1. $pattern = '#http://#'; 


See here for why: http://php.net/manual/en/regexp.referen ... miters.php


Top
 Profile  
 
PostPosted: Thu Apr 19, 2018 2:37 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13580
Location: New York, NY, US
Or escape the slashes:

Syntax: [ Download ] [ Hide ]
  1. $pattern = '/http:\/\//'; 


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

_________________
(#10850)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group