#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    218
    Rep Power
    1

    Question Why str_replace Not Working Properly With cURL?


    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' ?
    Last edited by UniqueIdeaMan; June 17th, 2017 at 06:15 PM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,286
    Rep Power
    630
    Post the script here using the proper tags. Most people will not go to a 3rd party site to read your unformatted code.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    218
    Rep Power
    1

    Cool


    Originally Posted by gw1500se
    Post the script here using the proper tags. Most people will not go to a 3rd party site to read your unformatted code.
    Opps! Sorry! I forgot to post the code!
    Given it in the next post.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    218
    Rep Power
    1
    Here is the code related to OP.

    Code:
    <?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://mymydomain.com/tracker.php?';
    
    $pattern = '/http:\/\//';
    $replacement = 'http://mymydomain.com/tracker.php?';
    
    $pattern = 'www.';
    $replacement = 'http://mymydomain.com/tracker.php?';
    
    echo str_replace($pattern, $replacement, $result);
    
    ?>
    Last edited by UniqueIdeaMan; June 18th, 2017 at 05:03 PM.
  8. #5
  9. Wiser? Not exactly.
    Devshed God 2nd Plane (6000 - 6499 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    6,026
    Rep Power
    4101
    1) str_replace doesn't do regular expressions, it's just straight string replacement. So you don't want delimiters on your pattern or escaped slashes.

    2) You're overwriting your pattern and replacement with new values then only doing the replacement on the last set of values. Either call str_replace multiple times or use arrays.
    Recycle your old CD's



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    218
    Rep Power
    1

    Question


    Originally Posted by kicken
    1) str_replace doesn't do regular expressions, it's just straight string replacement. So you don't want delimiters on your pattern or escaped slashes.

    2) You're overwriting your pattern and replacement with new values then only doing the replacement on the last set of values. Either call str_replace multiple times or use arrays.

    Ok. But I am calling str_replace multiple times like the following and it is still overwriting I reckon as it's still not working.

    Code:
    <?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 = '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);
    
    ?>

    Repeat: What I am trying to do is get cURL to pull-up the page: 'http://www.ebay.com'.
    Then, I'm trying to get the script to replace all the links' in the page. Replace all links' "http://" and "https://" and "www." with:

    'http://mymydomain.com/tracker.php?'.
    Last edited by UniqueIdeaMan; June 18th, 2017 at 05:19 PM.
  12. #7
  13. Wiser? Not exactly.
    Devshed God 2nd Plane (6000 - 6499 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    6,026
    Rep Power
    4101
    Originally Posted by UniqueIdeaMan
    Ok. But I am calling str_replace multiple times
    Really? Where? I only see one call and that is right here:

    Code:
    $pattern = 'www.';
    $replacement = 'http://mydomain.com/tracker.php?';
    
    echo str_replace($pattern, $replacement, $result);
    And according to your claim it's working fine in that one call (successfully replacing www.)
    Recycle your old CD's



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2017
    Posts
    13
    Rep Power
    0
    Your issue is already solved but for pattern replace you can use preg_replace function.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    218
    Rep Power
    1
    Originally Posted by robert4u
    Your issue is already solved but for pattern replace you can use preg_replace function.
    Preg_replace didn't work. There is another thread open on that. You may check it out:

    Preg_replace() Delimiter problem
    Last edited by UniqueIdeaMan; June 21st, 2017 at 10:55 AM.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    218
    Rep Power
    1
    Guys,

    I used the 1st code sample mentioned here:

    PHP: str_replace - Manual

    Called the $pattern etc. from an array to prevent overwriting.
    The code now looks like this:

    Code:
    <?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);
    
    $phrase  = "$result";
    $pattern = array("localhost","https://", "http://", "www.");
    $replace   = array("http://mymydomain.com/tracker.php?", "http://mymydomain.com/tracker.php?", "http://mymydomain.com/tracker.php?", "http://mymydomain.com/tracker.php?");
    
    $newphrase = str_replace($pattern, $replace, $phrase);
    
    echo $newphrase;
    
    ?>
    The original code was this and the replacements were getting overwritten by the next replacement:

    Code:
    <?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);
    
    
    ?>
    Anyway, there seems to be a small problem. I'm testing the script with ebay.com homepage. To understand the issue. Open 2 tabs.
    On one, head-over to ebay.com.
    On the other tab, run the following script on your NatePad++ and then run it on the 2nd tab (wamp/xampp). Now compare both tabs.
    What do you see strange on the 2nd tab ? The imgs are missing. Right ? Strange! Why did that happen ? Also, it is supposed to replace to 1 instance and not 2 of:
    'http://mymydomain.com/tracker.php?'.
    And so, hovering your mouse over any link in the 2nd tab, you will notice the links show 2 instances. Not good! Why did that happen ?
    Links will show like this:

    http://mymydomain.com/tracker.php?ht...some-file/html

    Remember, all I'm doing is getting the script to find 'http://', 'https://' and 'www.' and replace them all by:

    'http://mymydomain.com/tracker?'.

    In short, the script is preceding the 'http://mymydomain.com/tracker?' on all links. Since, links start with 'https://', 'http://' and 'www.', I got the script to change them.
    That way ...

    'https://ebay.com/some-file.jpeg' would be replaced to: 'http://mymydomain.com/tracker?https://ebay.com/some-file.jpeg'.

    That way ...

    'www.ebay.com/some-file.html' would be replaced to: 'http://mymydomain.com/tracker?www.ebay.com/some-file.html'.

    That way ...

    'http://ebay.com/some-file.php' would be replaced to: 'http://mymydomain.com/tracker?http://ebay.com/some-file.php'.
    Last edited by UniqueIdeaMan; June 21st, 2017 at 10:59 AM.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    218
    Rep Power
    1
    Folks,

    I've fixed the code but with a little hiccup. Took me 2hrs to fix it! Will try ridding the hiccup after I wake-up and then reveal the fix. :wink: In the meanwhile, it's my bed time. No good working now and make mistakes with a sleepy head. Stay tuned, my friend. Let's see what brings the next night! ...
    You will see how innovative I can become in a very tight corner.
    Thanks for all your helpS so far!
    You won't regret it! Here's my challenge to myself ...
    The final code would be a life saver for the world!

IMN logo majestic logo threadwatch logo seochat tools logo