Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/44473-trim-not-working-on-development-server/
Share on other sites

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  ');

No errors at all, just not changing the output... Sloppy code so don't laugh :P

 

$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...

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.

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  :)

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.