Jump to content


Photo

Trim variable from string


  • Please log in to reply
13 replies to this topic

#1 neoaddict

neoaddict
  • Members
  • PipPipPip
  • Advanced Member
  • 35 posts

Posted 25 August 2006 - 11:05 PM

How would I trim the value of a variable from a string in PHP?

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 26 August 2006 - 04:27 PM

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

$var = 'foo';

$str = 'This is foo Q speaking';

echo str_replace($var, '', $str);

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

#3 neoaddict

neoaddict
  • Members
  • PipPipPip
  • Advanced Member
  • 35 posts

Posted 27 August 2006 - 04:35 AM

$bk = file_get_contents("text.txt");
$bkup = explode("|", $wall);
$remove = "||";
str_replace($bkup[0], '', $bk);
str_replace($remove, '', $bk);

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


#4 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 27 August 2006 - 05:48 AM

Where does $wall come from? Where is $bk referenced? What does "won't work" mean?
Legend has it that reading the manual never killed anyone.
My site

#5 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 27 August 2006 - 09:29 AM

str_replace returns a new string so try
<?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 '||'
?>

or you can use a single str_replace
$bk = str_replace (array ($bkup[0], $remove), '', $bk);

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#6 neoaddict

neoaddict
  • Members
  • PipPipPip
  • Advanced Member
  • 35 posts

Posted 27 August 2006 - 04:49 PM

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

<?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
}
?>

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?

#7 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 27 August 2006 - 07:05 PM

For a start,

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

should be

$wall = file_get_contents("text.txt");
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#8 neoaddict

neoaddict
  • Members
  • PipPipPip
  • Advanced Member
  • 35 posts

Posted 27 August 2006 - 08:20 PM

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)

#9 neoaddict

neoaddict
  • Members
  • PipPipPip
  • Advanced Member
  • 35 posts

Posted 28 August 2006 - 04:32 PM

Bump.

#10 neoaddict

neoaddict
  • Members
  • PipPipPip
  • Advanced Member
  • 35 posts

Posted 29 August 2006 - 07:46 PM

Bump.

#11 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 29 August 2006 - 07:50 PM

Can you post examples of the data for each file? And how you expect the script to run?

Ken

#12 neoaddict

neoaddict
  • Members
  • PipPipPip
  • Advanced Member
  • 35 posts

Posted 30 August 2006 - 01:52 AM

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

|rainbow|brown|orange|

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.

#13 neoaddict

neoaddict
  • Members
  • PipPipPip
  • Advanced Member
  • 35 posts

Posted 30 August 2006 - 07:45 PM

Any suggestions?

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

#14 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 30 August 2006 - 08:31 PM

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.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users