Is there a function that trims trailing and leading newlines but NOT spaces?
Printable View
Is there a function that trims trailing and leading newlines but NOT spaces?
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.Quote:
Originally posted by nabeels786
PHP Code:$mystr=str_replace("\n","",$mystr);
I think if you add a second argument to the trim function it should work..
$trimmedStr = trim($theOriginalStr, "\n");
$mystr=str_replace("\r\n","",$mystr);
if that doesn't work give us an example of wht you are after.
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.Quote:
Originally posted by The Hobo
Is there a function that trims trailing and leading newlines but NOT 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:
Quote:
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.
If there's no function to do this, I'd probably have to write one:
I have the basic idea, I just need to fill in the blanks.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
}
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
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.
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:Quote:
Originally posted by scoutt
what is it that you are trying to do Hobo?
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)
Indeed. I guess I can't be so reliable on a secondary source. Thanks alot, GoldenShogun. :)Quote:
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])
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.
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.
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.Quote:
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
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?
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......
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...
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.Quote:
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.
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.
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" :rolleyes:
so, off with me...
oh, a \n isn't a new line?
Maybe not in your own personal terminology. :rolleyes:Quote:
"\n" (ASCII 10 (0x0A)), a new line (line feed).
from php.net
"\n" (ASCII 10 (0x0A)), a new line (line feed).
from php.net
hmmmmm wonder why it says new line. :confused:
what the hell are you talking about, dude? You make no sense...You're the one that said it wasn't a new line. :confused:
trying to tell you that your loop will not work if you check for newlines. the isn't is the key word.Quote:
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.
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.Quote:
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.
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):
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.Quote:
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.
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.
Okay, it's sloppy, but this is what I meant:
The result is here: http://www.vbshelf.com/test2.phpPHP 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>';
?>
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")
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...
:)
It probably would have only been a few bickering posts, but I can never understand what he's talking about...
Ah, the good old days :)