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

use the function eregi_replace();

eregi_replace ( string $pattern , string $replacement , string $string )

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.

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_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.

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

<?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);
?>

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

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

should be:

$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....

<!-- 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 -->
<?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:

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

Hope that helps you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.