Why str_replace Not Working Properly With cURL?
Posted: Sat Jun 17, 2017 6:07 pm
Php Buddies,
Why do you reckon the following script is unable to replace the 'https://' or the 'http://' words with 'http://mymydomain.com' ?
It is able to replace the words 'www.' with 'http://mymydomain.com', though.
Open 2 tabs in your browser where one opens to the page where you are running the following script in your wamp/xampp and the other tab should open direct to http://ebay.com for your experiment.
Running the script in your wamp/xampp and hovering your mouse over "fashion" link on ebay would show you 'http//mymydomain.com/tracker.php?ebay.com/global/fashion' and that is proof 'www.' got replaced.
You may confirm this cross checking both tabs by hovering your mouse over the mentioned links.
Now, in the (non xampp/wamp) tab, hover your mouse over the links "register", "sign in", etc. and you will see they start with 'https://'. Then finally, in the other tab (xampp/wamp), hover your mouse over these same links and you'd see the 'https://' have not been replaced with 'http://mymydomain.com'.
Why is that ?
Why is the str_replace failing on these 2 occasions to replace the 'http://' and the 'https://' to 'http://mymydomain.com' ?
Here is the code related to post 22 and so kindly answer my question in post 22 after reading it.
Also, don't forget to answer post 21 first.
Thank you everyone who attempts!
Why do you reckon the following script is unable to replace the 'https://' or the 'http://' words with 'http://mymydomain.com' ?
It is able to replace the words 'www.' with 'http://mymydomain.com', though.
Open 2 tabs in your browser where one opens to the page where you are running the following script in your wamp/xampp and the other tab should open direct to http://ebay.com for your experiment.
Running the script in your wamp/xampp and hovering your mouse over "fashion" link on ebay would show you 'http//mymydomain.com/tracker.php?ebay.com/global/fashion' and that is proof 'www.' got replaced.
You may confirm this cross checking both tabs by hovering your mouse over the mentioned links.
Now, in the (non xampp/wamp) tab, hover your mouse over the links "register", "sign in", etc. and you will see they start with 'https://'. Then finally, in the other tab (xampp/wamp), hover your mouse over these same links and you'd see the 'https://' have not been replaced with 'http://mymydomain.com'.
Why is that ?
Why is the str_replace failing on these 2 occasions to replace the 'http://' and the 'https://' to 'http://mymydomain.com' ?
Here is the code related to post 22 and so kindly answer my question in post 22 after reading it.
Also, don't forget to answer post 21 first.
Thank you everyone who attempts!
Code: Select all
<?php
$url = "http://www.ebay.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 can be either 2:
$pattern = '#http://#';
$pattern = '/http:\/\//';
*/
$pattern = '/https:\/\//';
$replacement = 'http://mydomain.com/tracker.php?';
$pattern = '/http:\/\//';
$replacement = 'http://mydomain.com/tracker.php?';
$pattern = 'www.';
$replacement = 'http://mydomain.com/tracker.php?';
echo str_replace($pattern, $replacement, $result);
?>
