| | |
Replacing
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
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...
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)
<?php function get_inner_string($a,$b,$c) { $y = explode($b,$a); $x = explode($c,$y[1]); return $x[0]; } $file = 'cache/94ddd435242c289003eca450b102df52.html'; $open_file = file_get_contents($file); $find_ad = get_inner_string($open_file, '<!-- Begin: AdBrite, Generated: 2008-12-18 16:23:11 -->', '<!-- End: AdBrite -->'); str_replace($find_ad, 'new ad'); ?>
•
•
Join Date: Dec 2008
Posts: 20
Reputation:
Solved Threads: 1
use the function eregi_replace();
eregi_replace ( string $pattern , string $replacement , string $string )
eregi_replace ( string $pattern , string $replacement , string $string )
-- Manish Singh
manish.s@neuralit.com
manish.s@neuralit.com
•
•
Join Date: Jul 2008
Posts: 149
Reputation:
Solved Threads: 25
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.
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.
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:
$new_ad should be whatever you want to put in-between the ad tags. Then you can do whatever you want with $new_result.
php Syntax (Toggle Plain Text)
$new_result = str_replace($find_ad, 'new ad', $new_ad);
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)
<?php function get_inner_string($a,$b,$c) { $y = explode($b,$a); $x = explode($c,$y[1]); return $x[0]; } $file = 'cache/94ddd435242c289003eca450b102df52.html'; $open_file = file_get_contents($file); $find_ad = get_inner_string($open_file, '<!-- Begin: AdBrite, Generated: 2008-12-18 16:23:11 -->', '<!-- End: AdBrite -->'); $new_ad = 'NEW AD'; $new_result = str_replace($new_ad, $find_ad, $open_file); echo $new_result; file_put_contents($open_file, $new_result); ?>
•
•
Join Date: Jul 2008
Posts: 149
Reputation:
Solved Threads: 25
for starters you have the usage of str_replace still mixed up.
http://us3.php.net/str_replace
should be:
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()
http://us3.php.net/str_replace
php Syntax (Toggle Plain Text)
$new_result = str_replace($new_ad, $find_ad, $open_file);
should be:
php Syntax (Toggle Plain Text)
$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()
Im not completly sure what regex is...
Here is the ad code you were asking for....
Here is the ad code you were asking for....
html Syntax (Toggle Plain Text)
<!-- Begin: AdBrite, Generated: 2008-12-18 16:23:11 --> <script type="text/javascript"> var AdBrite_Title_Color = '015990'; var AdBrite_Text_Color = '2D3F52'; var AdBrite_Background_Color = 'FFFFFF'; var AdBrite_Border_Color = 'CCCCCC'; var AdBrite_URL_Color = 'CC0000'; 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='';} </script> <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> <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> <!-- End: AdBrite -->
•
•
Join Date: Jul 2008
Posts: 149
Reputation:
Solved Threads: 25
php Syntax (Toggle Plain Text)
<?php $string = '<!-- Begin: AdBrite, Generated: 2008-12-18 16:23:11 --> <script type="text/javascript"> var AdBrite_Title_Color = \'015990\'; var AdBrite_Text_Color = \'2D3F52\'; var AdBrite_Background_Color = \'FFFFFF\'; var AdBrite_Border_Color = \'CCCCCC\'; var AdBrite_URL_Color = \'CC0000\'; 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=\'\';} </script> <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> <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> <!-- End: AdBrite -->'; $pattern = '/<!-- Begin: AdBrite, Generated: .*? -->((.|[\r\n])*?)<!-- End: AdBrite -->/'; $replacement = '<!-- Begin: AdBrite, Generated: '.date('Y-m-d H:i:s').' --> NEW AD <!-- End: AdBrite -->'; 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)
<!-- Begin: AdBrite, Generated: 2008-12-27 00:41:43 --> NEW AD <!-- End: AdBrite -->
Hope that helps you.
![]() |
Similar Threads
- Help: Replacing string with template (ColdFusion)
- How to perform Find and replacing in jTextpane (Java)
- C# replacing c++ as gaming language? (Game Development)
- Replacing file contents (Python)
- Ide #1 Error after replacing a dvd with an dvd/cd-rw (Storage)
- Replacing Hard Drive (Storage)
- replace printer w/o replacing whole system? (Apple Hardware)
- Replacing a field separator during a read. (Perl)
- Old IDE board needs replacing (Storage)
- Finding and Replacing Strings (C++)
Other Threads in the PHP Forum
- Previous Thread: Desktop Icon
- Next Thread: C,C++ compilers in own site
Views: 574 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access ajax apache api array beginner binary broken cakephp checkbox class cms code cron curl database date directory display download dynamic echo email error errors file files folder form forms function functions generator google href htaccess html image include insert integration ip java javascript joomla limit link login loop mail menu methods mlm mod_rewrite multiple mysql oop parse paypal pdf php phpincludeissue problem query radio random recursion recursive regex remote script search select server sessions shot sms soap source space speed sql structure syntax system table tutorial update updates upload url validation validator variable vbulletin video web xml youtube





