neoaddict Posted August 25, 2006 Share Posted August 25, 2006 How would I trim the value of a variable from a string in PHP? Quote Link to comment https://forums.phpfreaks.com/topic/18677-trim-variable-from-string/ Share on other sites More sharing options...
wildteen88 Posted August 26, 2006 Share Posted August 26, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/18677-trim-variable-from-string/#findComment-80781 Share on other sites More sharing options...
neoaddict Posted August 27, 2006 Author Share Posted August 27, 2006 [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. :( Quote Link to comment https://forums.phpfreaks.com/topic/18677-trim-variable-from-string/#findComment-81008 Share on other sites More sharing options...
AndyB Posted August 27, 2006 Share Posted August 27, 2006 Where does $wall come from? Where is $bk referenced? What does "won't work" mean? Quote Link to comment https://forums.phpfreaks.com/topic/18677-trim-variable-from-string/#findComment-81021 Share on other sites More sharing options...
Barand Posted August 27, 2006 Share Posted August 27, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/18677-trim-variable-from-string/#findComment-81058 Share on other sites More sharing options...
neoaddict Posted August 27, 2006 Author Share Posted August 27, 2006 Here's the code I've got so far:[code]<?php ob_start("ob_gzhandler"); ?><?phperror_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? Quote Link to comment https://forums.phpfreaks.com/topic/18677-trim-variable-from-string/#findComment-81151 Share on other sites More sharing options...
Barand Posted August 27, 2006 Share Posted August 27, 2006 For a start,$wall = file_get_contents(text.txt"); " missingshould be$wall = file_get_contents("text.txt"); Quote Link to comment https://forums.phpfreaks.com/topic/18677-trim-variable-from-string/#findComment-81211 Share on other sites More sharing options...
neoaddict Posted August 27, 2006 Author Share Posted August 27, 2006 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) Quote Link to comment https://forums.phpfreaks.com/topic/18677-trim-variable-from-string/#findComment-81238 Share on other sites More sharing options...
neoaddict Posted August 28, 2006 Author Share Posted August 28, 2006 Bump. Quote Link to comment https://forums.phpfreaks.com/topic/18677-trim-variable-from-string/#findComment-81686 Share on other sites More sharing options...
neoaddict Posted August 29, 2006 Author Share Posted August 29, 2006 Bump. Quote Link to comment https://forums.phpfreaks.com/topic/18677-trim-variable-from-string/#findComment-82417 Share on other sites More sharing options...
kenrbnsn Posted August 29, 2006 Share Posted August 29, 2006 Can you post examples of the data for each file? And how you expect the script to run?Ken Quote Link to comment https://forums.phpfreaks.com/topic/18677-trim-variable-from-string/#findComment-82420 Share on other sites More sharing options...
neoaddict Posted August 30, 2006 Author Share Posted August 30, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/18677-trim-variable-from-string/#findComment-82564 Share on other sites More sharing options...
neoaddict Posted August 30, 2006 Author Share Posted August 30, 2006 Any suggestions?Well, I echoed the variable that had the imploded array - it was exactly the way it was supposed to be. Quote Link to comment https://forums.phpfreaks.com/topic/18677-trim-variable-from-string/#findComment-83085 Share on other sites More sharing options...
Barand Posted August 30, 2006 Share Posted August 30, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/18677-trim-variable-from-string/#findComment-83106 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.