Replacing

Reply

Join Date: Dec 2007
Posts: 112
Reputation: !Unreal is an unknown quantity at this point 
Solved Threads: 2
!Unreal's Avatar
!Unreal !Unreal is offline Offline
Junior Poster

Replacing

 
0
  #1
Dec 23rd, 2008
Im tring to replace a string. But it just doesnt work for some reason. Ive tried str_replace and preg_replace but neither have worked.

My script basically caches a webpage then replaces a given string. The problem is replacing the string then saving it.

Could you please help me out...

  1. <?php
  2. function get_inner_string($a,$b,$c)
  3. {
  4. $y = explode($b,$a);
  5. $x = explode($c,$y[1]);
  6. return $x[0];
  7. }
  8.  
  9. $file = 'cache/94ddd435242c289003eca450b102df52.html';
  10.  
  11. $open_file = file_get_contents($file);
  12.  
  13. $find_ad = get_inner_string($open_file, '<!-- Begin: AdBrite, Generated: 2008-12-18 16:23:11 -->', '<!-- End: AdBrite -->');
  14.  
  15. str_replace($find_ad, 'new ad');
  16. ?>
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 20
Reputation: manish.s is an unknown quantity at this point 
Solved Threads: 1
manish.s manish.s is offline Offline
Newbie Poster

Re: Replacing

 
0
  #2
Dec 24th, 2008
use the function eregi_replace();

eregi_replace ( string $pattern , string $replacement , string $string )
-- Manish Singh
manish.s@neuralit.com
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 149
Reputation: mschroeder is on a distinguished road 
Solved Threads: 25
mschroeder mschroeder is offline Offline
Junior Poster

Re: Replacing

 
0
  #3
Dec 24th, 2008
steer clear of the eregi_* family of functions as of 5.3.0 they are depreciated.

You should be using the preg_* functions instead
http://us3.php.net/preg_replace

it looks like you're trying to read through the source code and replace whatever is between the ad comments with a new ad, but without seeing the actual code between those comments it would be hard to create a pattern that would work for you.
Last edited by mschroeder; Dec 24th, 2008 at 1:43 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 357
Reputation: death_oclock will become famous soon enough death_oclock will become famous soon enough 
Solved Threads: 37
death_oclock's Avatar
death_oclock death_oclock is offline Offline
Posting Whiz

Re: Replacing

 
0
  #4
Dec 24th, 2008
Maybe you should take a look at how str_replace is used: str_replace. You have to provide something to replace 'new_ad' with. Plus, the function doesn't store the new value in the place of the source string, it returns it so you need to store that somewhere. Try:
  1. $new_result = str_replace($find_ad, 'new ad', $new_ad);
$new_ad should be whatever you want to put in-between the ad tags. Then you can do whatever you want with $new_result.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 112
Reputation: !Unreal is an unknown quantity at this point 
Solved Threads: 2
!Unreal's Avatar
!Unreal !Unreal is offline Offline
Junior Poster

Re: Replacing

 
0
  #5
Dec 26th, 2008
Thanks for that. This is what Ive ended up with but it doesnt work. Could you help me out :p

  1. <?php
  2. function get_inner_string($a,$b,$c)
  3. {
  4. $y = explode($b,$a);
  5. $x = explode($c,$y[1]);
  6. return $x[0];
  7. }
  8.  
  9. $file = 'cache/94ddd435242c289003eca450b102df52.html';
  10.  
  11. $open_file = file_get_contents($file);
  12.  
  13. $find_ad = get_inner_string($open_file, '<!-- Begin: AdBrite, Generated: 2008-12-18 16:23:11 -->', '<!-- End: AdBrite -->');
  14.  
  15. $new_ad = 'NEW AD';
  16.  
  17. $new_result = str_replace($new_ad, $find_ad, $open_file);
  18.  
  19. echo $new_result;
  20.  
  21. file_put_contents($open_file, $new_result);
  22. ?>
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 149
Reputation: mschroeder is on a distinguished road 
Solved Threads: 25
mschroeder mschroeder is offline Offline
Junior Poster

Re: Replacing

 
0
  #6
Dec 26th, 2008
for starters you have the usage of str_replace still mixed up.
http://us3.php.net/str_replace

  1. $new_result = str_replace($new_ad, $find_ad, $open_file);

should be:

  1. $new_result = str_replace( $find_ad, $new_ad. $open_file );

The parameters for str_replace are search, replace, subject in your example you're searching for 'NEW AD' and trying to replace it with the result of the get_inner_string function.

Honestly though, this is the thing regular expressions are designed for, especially if the timestamp in "<!-- Begin: AdBrite, Generated: 2008-12-18 16:23:11 -->" changes.

But liked I said previously without seeing whats between those Ad Comments there is no use in trying to guess what kind of regex pattern would be necessary to do this with a simple call to preg_replace()
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 112
Reputation: !Unreal is an unknown quantity at this point 
Solved Threads: 2
!Unreal's Avatar
!Unreal !Unreal is offline Offline
Junior Poster

Re: Replacing

 
0
  #7
Dec 26th, 2008
Im not completly sure what regex is...

Here is the ad code you were asking for....

  1. <!-- Begin: AdBrite, Generated: 2008-12-18 16:23:11 -->
  2. <script type="text/javascript">
  3. var AdBrite_Title_Color = '015990';
  4. var AdBrite_Text_Color = '2D3F52';
  5. var AdBrite_Background_Color = 'FFFFFF';
  6. var AdBrite_Border_Color = 'CCCCCC';
  7. var AdBrite_URL_Color = 'CC0000';
  8. try{var AdBrite_Iframe=window.top!=window.self?2:1;var AdBrite_Referrer=document.referrer==''?document.location:document.referrer;AdBrite_Referrer=encodeURIComponent(AdBrite_Referrer);}catch(e){var AdBrite_Iframe='';var AdBrite_Referrer='';}
  9. </script>
  10. <span style="white-space:nowrap;"><script type="text/javascript">document.write(String.fromCharCode(60,83,67,82,73,80,84));document.write(' src="http://ads.adbrite.com/mb/text_group.php?sid=966656&zs=3732385f3930&ifr='+AdBrite_Iframe+'&ref='+AdBrite_Referrer+'" type="text/javascript">');document.write(String.fromCharCode(60,47,83,67,82,73,80,84,62));</script>
  11. <a target="_top" href="http://www.adbrite.com/mb/commerce/purchase_form.php?opid=966656&afsid=1"><img src="http://files.adbrite.com/mb/images/adbrite-your-ad-here-leaderboard.gif" style="background-color:#CCCCCC;border:none;padding:0;margin:0;" alt="Your Ad Here" width="14" height="90" border="0" /></a></span>
  12. <!-- End: AdBrite -->
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 149
Reputation: mschroeder is on a distinguished road 
Solved Threads: 25
mschroeder mschroeder is offline Offline
Junior Poster

Re: Replacing

 
0
  #8
Dec 27th, 2008
  1. <?php
  2.  
  3. $string = '<!-- Begin: AdBrite, Generated: 2008-12-18 16:23:11 -->
  4. <script type="text/javascript">
  5. var AdBrite_Title_Color = \'015990\';
  6. var AdBrite_Text_Color = \'2D3F52\';
  7. var AdBrite_Background_Color = \'FFFFFF\';
  8. var AdBrite_Border_Color = \'CCCCCC\';
  9. var AdBrite_URL_Color = \'CC0000\';
  10. try{var AdBrite_Iframe=window.top!=window.self?2:1;var AdBrite_Referrer=document.referrer==\'\'?document.location:document.referrer;AdBrite_Referrer=encodeURIComponent(AdBrite_Referrer);}catch(e){var AdBrite_Iframe=\'\';var AdBrite_Referrer=\'\';}
  11. </script>
  12. <span style="white-space:nowrap;"><script type="text/javascript">document.write(String.fromCharCode(60,83,67,82,73,80,84));document.write(\' src="http://ads.adbrite.com/mb/text_group.php?sid=966656&zs=3732385f3930&ifr=\'+AdBrite_Iframe+\'&ref=\'+AdBrite_Referrer+\'" type="text/javascript">\');document.write(String.fromCharCode(60,47,83,67,82,73,80,84,62));</script>
  13. <a target="_top" href="http://www.adbrite.com/mb/commerce/purchase_form.php?opid=966656&afsid=1"><img src="http://files.adbrite.com/mb/images/adbrite-your-ad-here-leaderboard.gif" style="background-color:#CCCCCC;border:none;padding:0;margin:0;" alt="Your Ad Here" width="14" height="90" border="0" /></a></span>
  14. <!-- End: AdBrite -->';
  15.  
  16. $pattern = '/<!-- Begin: AdBrite, Generated: .*? -->((.|[\r\n])*?)<!-- End: AdBrite -->/';
  17.  
  18. $replacement = '<!-- Begin: AdBrite, Generated: '.date('Y-m-d H:i:s').' -->
  19. NEW AD
  20. <!-- End: AdBrite -->';
  21.  
  22. echo preg_replace( $pattern, $replacement, $string );

This is pretty rough, and i think the pattern could probably be fixed up but it seems to match and work properly.

I assume you're using PHP5 not that there should be any difference, but if you run that you should see:

  1. <!-- Begin: AdBrite, Generated: 2008-12-27 00:41:43 -->
  2. NEW AD
  3. <!-- End: AdBrite -->

Hope that helps you.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 574 | Replies: 7
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC