|
-
Apr 29th, 2002, 09:32 PM
#1
Thread Starter
Stuck in the 80s
newlines?
Is there a function that trims trailing and leading newlines but NOT spaces?
-
Apr 30th, 2002, 05:58 AM
#2
Fanatic Member
PHP Code:
$mystr=str_replace("\n","",$mystr);
Visit www.fragblast.com
Gaming, forums, and a online RPG/Battle system
(__Flagg) DOT NET? is this a Hindi Dating service?
-
Apr 30th, 2002, 12:54 PM
#3
Thread Starter
Stuck in the 80s
Originally posted by nabeels786
PHP Code:
$mystr=str_replace("\n","",$mystr);
I want to strip TRAILING and LEADING newlines in a string, not all of them in the string.
-
May 1st, 2002, 07:45 AM
#4
Addicted Member
I think if you add a second argument to the trim function it should work..
$trimmedStr = trim($theOriginalStr, "\n");
-
May 1st, 2002, 09:24 AM
#5
$mystr=str_replace("\r\n","",$mystr);
if that doesn't work give us an example of wht you are after.
-
May 1st, 2002, 10:41 AM
#6
Thread Starter
Stuck in the 80s
Re: newlines?
Originally posted by The Hobo
Is there a function that trims trailing and leading newlines but NOT spaces?
I don't know if I can be more explainitory than this. I have a string that I want to strip LEADING and TRAILING newline characters, but NOT leading and trailing spaces.
Trim removes BOTH, and str_replace will remove all occurances of them in the string.
I hope this helps clear things up. TheGoldenShogun I don't think trim has a second parameter:
string trim (string str)
This function strips whitespace from the start and the end of a string and returns the stripped string. The whitespace characters it currently strips are: "\n", "\r", "\t", "\v", "\0", and a plain space.
-
May 1st, 2002, 10:44 AM
#7
Thread Starter
Stuck in the 80s
If there's no function to do this, I'd probably have to write one:
PHP Code:
function stripem($string) {
$x = strlen($string);
for ($i = 0; $i < $x; $i++) {
//cycle through each character
//if the character is a new line
//start the string after the current $i
//if it's not, break and exit loop
}
//do the same thing for the end of the string, somehow
}
I have the basic idea, I just need to fill in the blanks.
-
May 1st, 2002, 11:15 AM
#8
Addicted Member
Yeah the loop is probably the best way to go... I'm pretty sure it takes a second argument though
string trim ( string str [, string charlist])
Note: The optional charlist parameter was added in PHP 4.1.0
This function returns a string with whitespace stripped from the beginning and end of str. Without the second parameter, trim() will strip these characters: Without the second parameter, trim() will strip these characters:
" " (ASCII 32 (0x20)), an ordinary space.
"\t" (ASCII 9 (0x09)), a tab.
"\n" (ASCII 10 (0x0A)), a new line (line feed).
"\r" (ASCII 13 (0x0D)), a carriage return.
"\0" (ASCII 0 (0x00)), the NUL-byte.
"\x0B" (ASCII 11 (0x0B)), a vertical tab.
ex.
$trimmed = trim($text," \t.");
all this comes from php.net
http://www.php.net/manual/en/function.trim.php
-
May 1st, 2002, 11:20 AM
#9
Thread Starter
Stuck in the 80s
Oh, I looked here:
http://www.phpbuilder.com/manual/function.trim.php
They must not be up to date then. I'll try it when I get home from school.
-
May 1st, 2002, 11:57 AM
#10
what is it that you are trying to do Hobo?
-
May 1st, 2002, 12:48 PM
#11
Thread Starter
Stuck in the 80s
Originally posted by scoutt
what is it that you are trying to do Hobo?
As, I said. I don't know how to make myself clearer. Perhaps I have a problem with expressing myself but maybe an example would help:
My String (lets say $string): "\n\n The\nDog \n\n" I want to strip newline characters (\n), but now spaces ( ). So that my string would now be: " The\nDog "
str_replace would replace all newlines (I only want to replace the trailing and leading ones) and trim would replace the spaces before The and after Dog, which I want to preserve.
But I will give what TheGoldenShogun just pointed out a shot and see if it works (which I'm sure it will)
-
May 1st, 2002, 12:55 PM
#12
Thread Starter
Stuck in the 80s
Originally posted by TheGoldenShogun
Yeah the loop is probably the best way to go... I'm pretty sure it takes a second argument though
string trim ( string str [, string charlist])
Indeed. I guess I can't be so reliable on a secondary source. Thanks alot, GoldenShogun.
Last edited by The Hobo; Oct 4th, 2002 at 05:01 PM.
-
May 1st, 2002, 01:25 PM
#13
ok that makes sence. so basically you have this
"\n\n The\nDog \n\n" and you want it to look like this
"The\nDog"
so basically it would look like this
The
Dog
so I don't see the point in it, but not my concern. so if you have 2 \n\n and you know they are there why don't you do this
$mystr=str_replace("\n\n","",$mystr);
how are these being saved? are they coming from a database or they a text file.
-
May 1st, 2002, 01:33 PM
#14
Addicted Member
Yeah if you want all the info on something, go to the source, PHP.net is their official website... real easy to use, great search engine.
-
May 1st, 2002, 02:25 PM
#15
Thread Starter
Stuck in the 80s
Originally posted by scoutt
ok that makes sence. so basically you have this
"\n\n The\nDog \n\n" and you want it to look like this
"The\nDog"
so basically it would look like this
The
Dog
scoutt, I'm not going to waste anymore time trying to explain this to you. Read all the words in my post, don't just assume things. The keyword is SPACE. I don't want to remove the space.
Notice the difference between:
"The\nDog" and " The\nDog "
I know you're just trying to help me and everything, but if you're not going to make an attempt at reading my posts, don't try to help me. I don't need to explain myself every other post.
And furthermore, again I say, "$mystr=str_replace("\n\n","",$mystr);" Will replace all occurances of "\n" in $mystring. So that newline betwen The and Dog will be removed, and I don't want that.
Do you understand me now? Am I getting through to you?
-
May 1st, 2002, 02:56 PM
#16
pull your head out of your ass for just one moment. I asked those question for a reason. if you are adding it to a database then you might get away with some other code so you don't have to do what you are trying to do.
also this
$mystr=str_replace("\n\n","",$mystr);"
look real closely the \n\n is not \n. so you will be taking the \n\n out instead of \n. also you can add a space in there like so
$mystr=str_replace("\n\n"," ",$mystr);"
notice the space.??? it will replace all \n\n with a SPACE. dumb****but af course I don't know what I'm talking about......
-
May 1st, 2002, 03:14 PM
#17
Thread Starter
Stuck in the 80s
That's wonderful. Now what if I have "The\n\nDog?" It get's taken out it then too doesn't? I have no idea what the user is going to enter, but I have to preserve the spaces. So it will replace all occurances of '\n\n' in my string. Same problem.
Way to be such an overdramatic idiot though. It really works out great for you.
Now here's a spoon...
-
May 1st, 2002, 03:27 PM
#18
now if you would read my post I said if you know for a fact that there is only \n\n in it and the middle one is \n it would work, but since you don't know then that won't do the job. hmmmm if this is the case then your loop will not work either.
it sounds like an input field how are they going to enter a return in that. if a textarea then I wouldn't worry about it. I enter code in my textareas and I don't worry about it. but hey if you want the extra work then I won't help you.
but since you are too lazy then i will not tell you to use
ltrim - Strip whitespace from the beginning of a string
and
rtrim - Strip whitespace from the end of a string
later.
-
May 1st, 2002, 03:36 PM
#19
Thread Starter
Stuck in the 80s
Originally posted by scoutt
now if you would read my post I said if you know for a fact that there is only \n\n in it and the middle one is \n it would work, but since you don't know then that won't do the job. hmmmm if this is the case then your loop will not work either.
it sounds like an input field how are they going to enter a return in that. if a textarea then I wouldn't worry about it. I enter code in my textareas and I don't worry about it. but hey if you want the extra work then I won't help you.
but since you are too lazy then i will not tell you to use
ltrim - Strip whitespace from the beginning of a string
and
rtrim - Strip whitespace from the end of a string
later.
The loop thing would to have worked fine. What I was suggesting was that to go character by character and when the character ISN'T a new line, break from the loop.
I already got a working answer to this anyways. And I know of the ltrim and rtrim functions. they won't help me either. They kill spaces.
And since you don't know what my problem is, why are you suggesting that I don't worry about it and that it's going to be extra work for me? Don't second guess what I'm doing.
The user puts in input, it gets saved in a database, and later is formatted into color coded html to look like VB code does in the IDE. However, through this process an extra newline or possibly two is added to the end and beginings of the text. I simply wanted to strip them.
-
May 1st, 2002, 03:52 PM
#20
yup you are right, I am stupid. I use the same process and I don't have newlines at the beginning.
and your right about the loop if is doesn't detect a newline it would break out of the loop. of course this is not a newline "\n" 
so, off with me...
-
May 1st, 2002, 03:58 PM
#21
Thread Starter
Stuck in the 80s
oh, a \n isn't a new line?
"\n" (ASCII 10 (0x0A)), a new line (line feed).
from php.net
Maybe not in your own personal terminology.
-
May 1st, 2002, 04:30 PM
#22
"\n" (ASCII 10 (0x0A)), a new line (line feed).
from php.net
hmmmmm wonder why it says new line.
-
May 1st, 2002, 04:34 PM
#23
Thread Starter
Stuck in the 80s
what the hell are you talking about, dude? You make no sense...You're the one that said it wasn't a new line.
-
May 1st, 2002, 04:41 PM
#24
The loop thing would to have worked fine. What I was suggesting was that to go character by character and when the character ISN'T a new line, break from the loop.
trying to tell you that your loop will not work if you check for newlines. the isn't is the key word.
if you loop through this to detect \n (newline) it will take out all of them
"\n\n The\nDog \n\n"
same with this
"\n\n The\n\nDog \n\n"
your loop will do the same as this
$mystr=str_replace("\n"," ",$mystr);
so tell me the way you fixed it if you don't mind.
-
May 1st, 2002, 04:46 PM
#25
Thread Starter
Stuck in the 80s
Originally posted by scoutt
trying to tell you that your loop will not work if you check for newlines. the isn't is the key word.
if you loop through this to detect \n (newline) it will take out all of them
"\n\n The\nDog \n\n"
same with this
"\n\n The\n\nDog \n\n"
your loop will do the same as this
$mystr=str_replace("\n"," ",$mystr);
so tell me the way you fixed it if you don't mind.
Okay, obviously we aren't understanding each other at all.
I was saying that I would loop through the text and trim it down each time until the current character is not a new line.
Lets say we have "\n\n The\n\nDog \n\n"
It would go character by character (assuming that a new line is counted as one character):
Is \n a new line? Yes, trim the string to start at 1.
Is \n a new line? Yes, trim the string to start at 1.
Is a new line? No, it's a space, break from the loop.
I guess I'll write up the example to show you what I mean. I fixed it by using Trim with the second parameter as stated by TheGoldenShogun.
-
May 1st, 2002, 04:54 PM
#26
hehe you are funny.
what did you plan on using in your loop?
str_replace or trim.
either one would do fine without the loop. if you didn't use any of those then you would never reach the end of the string. you would always stop at " The" . if the character is not a new line then exit. that is all I was saying.
-
May 1st, 2002, 04:56 PM
#27
Thread Starter
Stuck in the 80s
Okay, it's sloppy, but this is what I meant:
PHP Code:
<?php
$ourstring = "\n\n The\n\n Dog \n\n";
$x = strlen($ourstring);
echo 'Before:<pre style="background-color: silver;">' . $ourstring .'</pre>';
for ($i = 0; $i < $x; $i++) {
if (substr($ourstring, 0, 1) == "\n") {
$ourstring = substr($ourstring, 1);
} else {
break;
}
}
$x = strlen($ourstring);
for ($i = 0; $i < $x; $i++) {
if (substr($ourstring, $x - 1, 1) == "\n") {
$ourstring = substr($ourstring, 0, $x - 1);
$x = strlen($ourstring);
} else {
break;
}
}
echo 'After:<pre style="background-color: silver;">' . $ourstring .'</pre>';
?>
The result is here: http://www.vbshelf.com/test2.php
It scans through the begining of the string, character by character and trimming it down until it comes across a character that is not a new line. Then it does the same thing at the end.
But the same thing can be accomplished with Trim($ourstring, "\n")
-
May 2nd, 2002, 06:06 AM
#28
Addicted Member
Geez, listen to you two go back and forth. We got a little cat fight going on in the forum. And yesterday was the 10 year anniversary of Rodney King's "Cant we all just get along?" speech... he he, it's always fun to have a simple post turn into a 27-28 message arguement every now and then. Hey, we're programmers, we gotta relieve stress on one another every now and then...
-
May 2nd, 2002, 10:42 AM
#29
Thread Starter
Stuck in the 80s
It probably would have only been a few bickering posts, but I can never understand what he's talking about...
-
Oct 4th, 2002, 05:03 PM
#30
Thread Starter
Stuck in the 80s
Ah, the good old days
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|