943,708 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 723
  • PHP RSS
Dec 23rd, 2008
0

Replacing

Expand Post »
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...

php Syntax (Toggle Plain Text)
  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. ?>
Similar Threads
Reputation Points: 11
Solved Threads: 2
Junior Poster
!Unreal is offline Offline
112 posts
since Dec 2007
Dec 24th, 2008
0

Re: Replacing

use the function eregi_replace();

eregi_replace ( string $pattern , string $replacement , string $string )
Reputation Points: 10
Solved Threads: 5
Light Poster
manish.s is offline Offline
29 posts
since Dec 2008
Dec 24th, 2008
0

Re: Replacing

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.
Sponsor
Reputation Points: 265
Solved Threads: 126
Practically a Master Poster
mschroeder is offline Offline
624 posts
since Jul 2008
Dec 24th, 2008
0

Re: Replacing

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:
php Syntax (Toggle Plain Text)
  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.
Reputation Points: 128
Solved Threads: 43
Posting Whiz
death_oclock is offline Offline
389 posts
since Apr 2006
Dec 26th, 2008
0

Re: Replacing

Thanks for that. This is what Ive ended up with but it doesnt work. Could you help me out :p

php Syntax (Toggle Plain Text)
  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. ?>
Reputation Points: 11
Solved Threads: 2
Junior Poster
!Unreal is offline Offline
112 posts
since Dec 2007
Dec 26th, 2008
0

Re: Replacing

for starters you have the usage of str_replace still mixed up.
http://us3.php.net/str_replace

php Syntax (Toggle Plain Text)
  1. $new_result = str_replace($new_ad, $find_ad, $open_file);

should be:

php Syntax (Toggle Plain Text)
  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()
Sponsor
Reputation Points: 265
Solved Threads: 126
Practically a Master Poster
mschroeder is offline Offline
624 posts
since Jul 2008
Dec 26th, 2008
0

Re: Replacing

Im not completly sure what regex is...

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

html Syntax (Toggle Plain Text)
  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 -->
Reputation Points: 11
Solved Threads: 2
Junior Poster
!Unreal is offline Offline
112 posts
since Dec 2007
Dec 27th, 2008
0

Re: Replacing

php Syntax (Toggle Plain Text)
  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:

html Syntax (Toggle Plain Text)
  1. <!-- Begin: AdBrite, Generated: 2008-12-27 00:41:43 -->
  2. NEW AD
  3. <!-- End: AdBrite -->

Hope that helps you.
Sponsor
Reputation Points: 265
Solved Threads: 126
Practically a Master Poster
mschroeder is offline Offline
624 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Desktop Icon
Next Thread in PHP Forum Timeline: C,C++ compilers in own site





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC