Jump to content

Archived

This topic is now archived and is closed to further replies.

neoaddict

Trim variable from string

Recommended Posts

by trim you mean remove. If you do then use str_replace

[code=php:0]$var = 'foo';

$str = 'This is foo Q speaking';

echo str_replace($var, '', $str);[/code]

If not could you provide more info on what you are trying to do. Along with an example.

Share this post


Link to post
Share on other sites
[code]$bk = file_get_contents("text.txt");
$bkup = explode("|", $wall);
$remove = "||";
str_replace($bkup[0], '', $bk);
str_replace($remove, '', $bk);[/code]

That's sort of the general idea right now, but it won't work.  :(

Share this post


Link to post
Share on other sites
Where does $wall come from? Where is $bk referenced? What does "won't work" mean?

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites
For a start,

$wall = file_get_contents(text.txt");    " missing

should be

$wall = file_get_contents("text.txt");

Share this post


Link to post
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)

Share this post


Link to post
Share on other sites
Can you post examples of the data for each file? And how you expect the script to run?

Ken

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
Any suggestions?

Well, I echoed the variable that had the imploded array - it was exactly the way it was supposed to be.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

×

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.