Jump to content

Trim variable from string


neoaddict

Recommended Posts

str_replace returns a new string so try
[code]<?php
$bk = file_get_contents("text.txt");
$bkup = explode("|", $wall);
$remove = "||";
$bk = str_replace($bkup[0], '', $bk);  // remove all occurences of first element in $bkup
                                                  //  EG if $wall = "foo|bar" then remove "foo"
$bk = str_replace($remove, '', $bk);  //  remove all occurences of '||'
?>[/code]

or you can use a single str_replace
[code]
$bk = str_replace (array ($bkup[0], $remove), '', $bk);
[/code]
Link to comment
Share on other sites

Here's the code I've got so far:

[code]<?php ob_start("ob_gzhandler"); ?>
<?php

error_reporting(E_ALL ^ E_WARNING);

//*********************
// Config!  o_O
//*********************
$wall = file_get_contents(text.txt");
$bk = file_get_contents("text.txt");
$walldef = file_get_contents("deftext.txt");
$year = date("Y");
$pieces = explode("|", $wall);
$bkup = explode("|", $bk);
$slices = explode("|", $walldef);
$story = "<font size='2' face='Verdana'>";
$gotnews = "";
$time = date("U");

$fp = fopen("year.txt", "w");
fwrite($fp, $year);
fclose($fp);

if ($year == $year + 1) {
$itsnewyear = fopen("text.txt", "w");
fwrite($itsnewyear, $walldef);
fclose($itsnewyear);
}

foreach ($pieces as $run){
if( !fopen("http://images.neopets.com/games/new_tradingcards/sm_".$run."_day_".$year.".gif", "r") )
{
$newsvar = false;
}
else{
$newsvar = true;
$gotnews = "Confirmedsir";
$ca = file_get_contents("test2.txt");
$comment = fopen("test2.txt", "w");
fwrite($comment, "$time|>|\n$ca");
fclose($comment);
}

if ($newsvar == true && $gotnews == "Confirmedsir"){
$story = "<b><i>Wallpaper:</b></i><br /><br />";
}

$newsvar = false;

if ($newsvar != true && $gotnews == "Confirmedsir"){
$story .= "<img src=\"http://images.neopets.com/games/new_tradingcards/sm_".$run."_day_".$year.".gif\">";
$story .= "<br /><br />";

$write = fopen("3_Wallpaper.txt", 'w');

$bk = str_replace('||', '', $bk);
unset($bkup[0]);
$bkup = array_values($bkup);
print_r($bkup);
$bkup = implode("|", $bkup);
fwrite($write, $bkup);
fclose($write);
}
}

if($gotnews == "Confirmedsir") {
$category = "2";
$time = date("U");

$newsa = file_get_contents("test.txt");
$newsfile = fopen("test.txt", "w");
fwrite($newsfile, "$time|$me|$title|$story||$av|$category||\n$newsa");
fclose($newsfile);
}
else{
// Nothing
}
?>[/code]

OK, I got it to remove the first object of the array, but it only does it once instead of multiple times as it should in a foreach?
Link to comment
Share on other sites

Oops, a little mistake while I was altering the file paths to post on PHP Freaks.

I got it to remove the first part of the array, got it to implode nicely, but:

* Won't process multiple times (did I get the meaning of foreach wrong?  Shouldn't it be processed every time the loop, well, loops?)

* Wipes file (Doesn't write anything to the file)
Link to comment
Share on other sites

This is an example of data of the only critical file (script gets data to loop from this file):

[code]|rainbow|brown|orange|[/code]

It keeps on either writing Array or nothing to the file.

I expect the script to loop the data from the text.txt file, check if it exists, if it does, unset the first item of the array, reindex the array, reimplode the data and write it to a file.
Link to comment
Share on other sites

Here is some advice, take it or leave it.

Posting code that "doesn't work" does not tell us what you are attempting to do. Also, Posting badly formatted code, (which, for example, makes it impossible to tell where the foreach() loop begins and ends without going though the code to count start and ending {}'s ) is going to send people to another topic.

So if you were to indent you code properly and put comments in (every line if necessary) to explain what you are trying to do I suspect your response rate will rise. Who knows, in explaining it to yourself you may even spot the errors.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.