RE: [PHP] Reg ex help-Removing extra blank spaces before HTML output
cool, glad you got it working...if you think there's a bug with using / and | you might want to see if you can replicate the problem, and if its really a bug, submit it...my guess is that it was just a typo or something jack -Original Message- From: Ken [mailto:[EMAIL PROTECTED]] Sent: Thursday, December 06, 2001 5:13 PM To: Jack Dempsey; PHP list Subject: RE: [PHP] Reg ex help-Removing extra blank spaces before HTML output OK, this time the \n worked. The only thing I changed was using / delimiters instead of | delimiters in the search string. No idea if/why that would affect anything in the replacement string. Anyway, I finally came up with exactly what I wanted: preg_replace(array("/\s*\n+\s*/", "/[ ]+/"), array("\n", " "), $input); All padding (defined as any series of spaces, newlines and sometimes tabs) is removed from $input, but always leaving at least a space or a newline in its place. If there were any newlines in the "padding", then a newline is left. If there weren't, then a space is left. Thanks for the help! Ken [EMAIL PROTECTED] At 04:40 PM 12/6/01 -0500, Jack Dempsey wrote: >one call to preg_replace...if you check out php.net/preg_replace, you'll see >what i mean...use arrays... >and as long as you have them ordered in the right way, it'll execute them in >that order, so by the time the \s replace comes up all your 'bbbnbb' will be >fixed...problem then is that those newlines would get caught by the \s and >get switched to spaces...so, if you want them to stay, then use [ ] to >represent a space instead of \s...might want [ \t] to catch tabs as well... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] Reg ex help-Removing extra blank spaces before HTML output
OK, this time the \n worked. The only thing I changed was using / delimiters instead of | delimiters in the search string. No idea if/why that would affect anything in the replacement string. Anyway, I finally came up with exactly what I wanted: preg_replace(array("/\s*\n+\s*/", "/[ ]+/"), array("\n", " "), $input); All padding (defined as any series of spaces, newlines and sometimes tabs) is removed from $input, but always leaving at least a space or a newline in its place. If there were any newlines in the "padding", then a newline is left. If there weren't, then a space is left. Thanks for the help! Ken [EMAIL PROTECTED] At 04:40 PM 12/6/01 -0500, Jack Dempsey wrote: >one call to preg_replace...if you check out php.net/preg_replace, you'll see >what i mean...use arrays... >and as long as you have them ordered in the right way, it'll execute them in >that order, so by the time the \s replace comes up all your 'bbbnbb' will be >fixed...problem then is that those newlines would get caught by the \s and >get switched to spaces...so, if you want them to stay, then use [ ] to >represent a space instead of \s...might want [ \t] to catch tabs as well... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] Reg ex help-Removing extra blank spaces before HTML output
second issue first: one call to preg_replace...if you check out php.net/preg_replace, you'll see what i mean...use arrays... and as long as you have them ordered in the right way, it'll execute them in that order, so by the time the \s replace comes up all your 'bbbnbb' will be fixed...problem then is that those newlines would get caught by the \s and get switched to spaces...so, if you want them to stay, then use [ ] to represent a space instead of \s...might want [ \t] to catch tabs as well... first issue: hard to see whats going on, but check your syntax. i just ran this; $text = "this is some text with a newline in it right here \n there it was"; echo $text; echo ''; echo preg_replace("/[ ]+\n+[ ]+/","\n",$text); and it made the substitution fine. jack -Original Message- From: Ken [mailto:[EMAIL PROTECTED]] Sent: Thursday, December 06, 2001 1:56 PM To: Jack Dempsey Subject: RE: [PHP] Reg ex help-Removing extra blank spaces before HTML output At 11:41 AM 12/6/01 -0500, Jack Dempsey wrote: >using "\n" as your replacement text will do ithowever, if you want a >newline in the html, then you'll want ... Using "\n" (either in single or double quotes) resulted in actual 'backslash n' being all over my output, instead of newlines. Hence, my inquiry. >your first pattern will be "/[ ]+\n+[ ]+/" with a replacement of a single >newline...then the second will be the previous regex.check out php.net >for help with syntax and using arrays with preg_* functionsthe first >should catch all situations of a series of spaces [with at least one newline >in between them] and substitute a newline. Thanks, sounds about right. The second pattern (which sometimes includes getting rid of newlines) won't clobber the results of the first pattern? And are you suggesting two separate calls to the preg* function? It sounds like you are, if each of the two patterns also has a unique replacement string. Thanks a lot! - Ken [EMAIL PROTECTED] >check out mastering regular expressions as well.its worth the money >jack > >-Original Message- >From: Ken [mailto:[EMAIL PROTECTED]] > >At 02:43 PM 12/5/01 -0500, Jack Dempsey wrote: > >$t = preg_replace('/\s+/',' ',$text); > >One more thing: >How do I replace with newline instead of a space? I read through the >manuals but couldn't grasp how to do this. \n didn't cut it. > >And, more advanced - >The above replaces any groups of 2 or more blanks (newlines, spaces, tabs) >with a single blank. >Is there an easy way to replace them with EITHER a space or a newline, >depending on whether any newlines were present in the input? >I.e. bb would be replaced with b, but bnbn would be >replaced with n. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] Reg ex help-Removing extra blank spaces before HTML output
using "\n" as your replacement text will do ithowever, if you want a newline in the html, then you'll want ... and as far as the advanced replace, make an array of search patterns, in this case two. your first pattern will be "/[ ]+\n+[ ]+/" with a replacement of a single newline...then the second will be the previous regex.check out php.net for help with syntax and using arrays with preg_* functionsthe first should catch all situations of a series of spaces [with at least one newline in between them] and substitute a newline. check out mastering regular expressions as well.its worth the money jack -Original Message- From: Ken [mailto:[EMAIL PROTECTED]] Sent: Thursday, December 06, 2001 3:08 AM To: Jack Dempsey; liljim; PHP list Subject: RE: [PHP] Reg ex help-Removing extra blank spaces before HTML output At 02:43 PM 12/5/01 -0500, Jack Dempsey wrote: >$t = preg_replace('/\s+/',' ',$text); One more thing: How do I replace with newline instead of a space? I read through the manuals but couldn't grasp how to do this. \n didn't cut it. And, more advanced - The above replaces any groups of 2 or more blanks (newlines, spaces, tabs) with a single blank. Is there an easy way to replace them with EITHER a space or a newline, depending on whether any newlines were present in the input? I.e. bb would be replaced with b, but bnbn would be replaced with n. Right now the issue is that I'm sometimes getting results that have no line breaks for a really long time, and I know there are reasons to try to avoid individual lines of HTML longer than 200 characters. Thanks! - Ken [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] Reg ex help-Removing extra blank spaces before HTML output
At 02:43 PM 12/5/01 -0500, Jack Dempsey wrote: >$t = preg_replace('/\s+/',' ',$text); One more thing: How do I replace with newline instead of a space? I read through the manuals but couldn't grasp how to do this. \n didn't cut it. And, more advanced - The above replaces any groups of 2 or more blanks (newlines, spaces, tabs) with a single blank. Is there an easy way to replace them with EITHER a space or a newline, depending on whether any newlines were present in the input? I.e. bb would be replaced with b, but bnbn would be replaced with n. Right now the issue is that I'm sometimes getting results that have no line breaks for a really long time, and I know there are reasons to try to avoid individual lines of HTML longer than 200 characters. Thanks! - Ken [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] Reg ex help-Removing extra blank spaces before HTML output
actually it does... james' solution will work but I forgot to mention a \s is whitespace, not a space, therefore it will get tabs, newlines, and spaces. look at http://www.cs.tut.fi/~jkorpela/perl/regexp.html " \s matches any whitespace character (space, tab, newline)" therefore my suggestion works: $t = preg_replace('/\s+/',' ',$text); its quicker to code, easier to understand, and will run faster because it doesn't have to match and remember or do alternation... jack -Original Message- From: Ken [mailto:[EMAIL PROTECTED]] Sent: Wednesday, December 05, 2001 2:01 PM To: Jack Dempsey Subject: RE: [PHP] Reg ex help-Removing extra blank spaces before HTML output At 10:57 AM 12/5/01 -0500, Jack Dempsey wrote: >a space (\s is from perl) Ah, OK, yours doesn't deal with newlines. From list member "James", a solution that does: - From: "liljim" <[EMAIL PROTECTED]> The example Jack gave you will clear up spaces well, though to get both newlines and spaces into one: $input = preg_replace("/([ ]|\n){1,}/", "\\1", $input); - Thanks again! - Ken [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] Reg ex help-Removing extra blank spaces before HTML output
true...he didn't mention newlines though so i left those out...also, "since PHP 4.0.4 $n [meaning $1], being the preferred one." you also don't need double quotes and since + means 1 or more, i'd save myself some typing $input = preg_replace('/(\s|\n)+/',"$1",$input); of course since you're now matching and replacing it'll take longer than a straight substitution, although i doubt the files you're scanning are big enough to notice jack -Original Message- From: liljim [mailto:[EMAIL PROTECTED]] Sent: Wednesday, December 05, 2001 5:18 AM To: [EMAIL PROTECTED] Subject: Re: [PHP] Reg ex help-Removing extra blank spaces before HTML output Hello, The example Jack gave you will clear up spaces well, though to get both newlines and spaces into one: $input = preg_replace("/([ ]|\n){1,}/", "\\1", $input); James "Jack Dempsey" <[EMAIL PROTECTED]> wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... > $text = preg_replace('|\s+|',' ',$text); > > > -Original Message- > From: Ken [mailto:[EMAIL PROTECTED]] > Sent: Wednesday, December 05, 2001 2:06 AM > To: PHP list > Subject: [PHP] Reg ex help-Removing extra blank spaces before HTML > output > > > I want to remove all superfluous blank spaces before I sent my HTML output, > to make the output smaller. > > So I'd like to take $input, replace any number of blank space or newlines > that are consecutive and replace them with a single blank. > > I.e. I will list a blank space as b and a newline as n: > > If input is: bTEXTHEREbbbnnnbnMOREbEVENMORE > Then output should be: bTEXTHEREbMOREbEVENMORE > > I imagine this would be handled by a simple regular expression call, but I'm > no pro with them yet. > > Any help? > > Thanks, > Ken > [EMAIL PROTECTED] > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > To contact the list administrators, e-mail: [EMAIL PROTECTED] > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] Reg ex help-Removing extra blank spaces before HTML output
Hello, The example Jack gave you will clear up spaces well, though to get both newlines and spaces into one: $input = preg_replace("/([ ]|\n){1,}/", "\\1", $input); James "Jack Dempsey" <[EMAIL PROTECTED]> wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... > $text = preg_replace('|\s+|',' ',$text); > > > -Original Message- > From: Ken [mailto:[EMAIL PROTECTED]] > Sent: Wednesday, December 05, 2001 2:06 AM > To: PHP list > Subject: [PHP] Reg ex help-Removing extra blank spaces before HTML > output > > > I want to remove all superfluous blank spaces before I sent my HTML output, > to make the output smaller. > > So I'd like to take $input, replace any number of blank space or newlines > that are consecutive and replace them with a single blank. > > I.e. I will list a blank space as b and a newline as n: > > If input is: bTEXTHEREbbbnnnbnMOREbEVENMORE > Then output should be: bTEXTHEREbMOREbEVENMORE > > I imagine this would be handled by a simple regular expression call, but I'm > no pro with them yet. > > Any help? > > Thanks, > Ken > [EMAIL PROTECTED] > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > To contact the list administrators, e-mail: [EMAIL PROTECTED] > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] Reg ex help-Removing extra blank spaces before HTML output
$text = preg_replace('|\s+|',' ',$text); -Original Message- From: Ken [mailto:[EMAIL PROTECTED]] Sent: Wednesday, December 05, 2001 2:06 AM To: PHP list Subject: [PHP] Reg ex help-Removing extra blank spaces before HTML output I want to remove all superfluous blank spaces before I sent my HTML output, to make the output smaller. So I'd like to take $input, replace any number of blank space or newlines that are consecutive and replace them with a single blank. I.e. I will list a blank space as b and a newline as n: If input is: bTEXTHEREbbbnnnbnMOREbEVENMORE Then output should be: bTEXTHEREbMOREbEVENMORE I imagine this would be handled by a simple regular expression call, but I'm no pro with them yet. Any help? Thanks, Ken [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]