ste1986 Posted March 27, 2007 Share Posted March 27, 2007 Hi, I've got a very weird problem! I'm writing a script that will manipulate some pasted text... most of the code uses trim() to clear characters/words from the beginnings of lines. It doesn't work on it's own trim($string) or with the seperate argument trim($string , "text"). The code is fine, it works ok one of our paid for hosting servers (pre-configured), but it won't work on either of our dev servers! All are using Apache. Version info: Hosting Server (Linux - works)... PHP 4.4.6 Dev 1 (Linux - doesnt work)... PHP 4.3.9 Dev 2 (Windows doesnt work)... PHP 5.2.1 It's stumped me! I always assumed that something as simple as trim() would be enabled by default. Can anyone shed any light? Quote Link to comment https://forums.phpfreaks.com/topic/44473-trim-not-working-on-development-server/ Share on other sites More sharing options...
Jenk Posted March 27, 2007 Share Posted March 27, 2007 what errors (if any) are you receiving? What code are you using? Quote Link to comment https://forums.phpfreaks.com/topic/44473-trim-not-working-on-development-server/#findComment-216007 Share on other sites More sharing options...
jitesh Posted March 27, 2007 Share Posted March 27, 2007 If you are facing a prb because of a version then try this code function trim_str($str){ if(strlen($str) > 0){ $str = preg_replace("/^\s+/","",$str); $str = preg_replace("/\s+$/","",$str); return $str; } return $str; } echo "This is trimed string".trim_str(' test '); Quote Link to comment https://forums.phpfreaks.com/topic/44473-trim-not-working-on-development-server/#findComment-216022 Share on other sites More sharing options...
jitesh Posted March 27, 2007 Share Posted March 27, 2007 This is in javascript <script language="javascript"> function trim() { if (this.length > 0) { var retstr = this.replace(/^\s+/,""); retstr = retstr.replace(/\s+$/,""); return retstr; } else return this; } </script> Quote Link to comment https://forums.phpfreaks.com/topic/44473-trim-not-working-on-development-server/#findComment-216024 Share on other sites More sharing options...
Jenk Posted March 27, 2007 Share Posted March 27, 2007 both of those solutions have their own problems. Let's try actually fixing the problem.. Quote Link to comment https://forums.phpfreaks.com/topic/44473-trim-not-working-on-development-server/#findComment-216027 Share on other sites More sharing options...
ste1986 Posted March 27, 2007 Author Share Posted March 27, 2007 No errors at all, just not changing the output... Sloppy code so don't laugh $text1 = "Loads of view multi line code etc etc etc etc etc" $text2 = strtok("$text1" , "\n"); $count = 1; while($count <= 10) { Print($text2 . "<BR>"); /* $list1 = $list1 . "'" . $text2 . "', "; */ $array1[$count] = $text2; $text2 = strtok("\n"); $count = $count + 1; } $result1 = $array1[1]; $result1 = rtrim($result1, "view"); Print($result1."<br>"); Print($array1[2]."<br>"); Print($array1[3] ."<br>"); Print($array1[4] ."<br>"); Print($array1[5] ."<br>"); Print($array1[6] ."<br>"); Print($array1[7] ."<br>"); Print($array1[8] ."<br>"); Print($array1[9] ."<br>"); Print($array1[10] ."<br>"); So in that one, I just want it to trim the word view from the first line, but it doesnt do it on my dev servers... Quote Link to comment https://forums.phpfreaks.com/topic/44473-trim-not-working-on-development-server/#findComment-216029 Share on other sites More sharing options...
Jenk Posted March 27, 2007 Share Posted March 27, 2007 rtrim() only trims from the end of string (i.e. $var = rtrim("view some text view", "view"); echo $var; //outputs "view some text" ) - is that what you want? Quote Link to comment https://forums.phpfreaks.com/topic/44473-trim-not-working-on-development-server/#findComment-216032 Share on other sites More sharing options...
ste1986 Posted March 27, 2007 Author Share Posted March 27, 2007 In this instance yes, just text from the end of the file. It does the same if I use trim() as well. Sorry for the code, it's from a file I've just been using to test different methods. Quote Link to comment https://forums.phpfreaks.com/topic/44473-trim-not-working-on-development-server/#findComment-216034 Share on other sites More sharing options...
Jenk Posted March 27, 2007 Share Posted March 27, 2007 No problem.. I was checking you were not expecting rtrim() to do what trim() does is all. Try echoing the native value inside of tokens (e.g. ") before echoing the trim'd value to compare. Chances are the end of the string does not match so it's not trimming. Quote Link to comment https://forums.phpfreaks.com/topic/44473-trim-not-working-on-development-server/#findComment-216037 Share on other sites More sharing options...
ste1986 Posted March 27, 2007 Author Share Posted March 27, 2007 Before trim, it outputs (with my data in): <br>Chris, Atkins view <br> There's no space at the end of the line, just a carriage return I'm assuming. So doing rtrim($text, "view") should remove it? Quote Link to comment https://forums.phpfreaks.com/topic/44473-trim-not-working-on-development-server/#findComment-216041 Share on other sites More sharing options...
ste1986 Posted March 27, 2007 Author Share Posted March 27, 2007 Panic over! Sorry to waste your time guys... it looks like it was either a problem with my code or the text I was pasting in! I changed it to: $result1 = trim($result1); $result1 = trim($result1, "view"); $result1 = trim($result1); It now works fine... weird that it worked ok on the other server though! Must just be a random setting. I'll post back if I figure out what the setting is! Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/44473-trim-not-working-on-development-server/#findComment-216050 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.