Re: [PHP] Reg-ex help
Thanks! That's a big help. - Craige Jim Lucas wrote: Craige Leeder wrote: Hey guys, I'm trying to write a regular expression to match a tag for my frameworks template engine. I seem to be having some trouble. The expression should match: {:seg 'segname':} {:seg 'segname' cache:} What I have is... $fSegRegEx = "#\{:seg \'[a-z0-9\-\_]{3,}\'( cache)?:\}#i"; Which when run against my test data, seems to match: "{:seg 'segname' cache:}" " cache" Thanks guys. This has been bugging me for a couple days. - Craige I used some of your code, but try this variant on for size. {:seg 'title':} {:seg 'title' cache:} DATA; # For arguments sake, I'll provide the whole code snippet... $fSegRegEx = "|\{:(seg) \'([a-z0-9\-\_]{3,})\'\s?(cache)?:\}|i"; preg_match_all($fSegRegEx, $dummyData, $faSegMatches, PREG_SET_ORDER); print_r($faSegMatches); ?> Not sure what your input is going to be (HTML, XML, etc...) so I used HTML Anyways, output from the above code is this: Array ( [0] => Array ( [0] => {:seg 'title':} [1] => seg [2] => title ) [1] => Array ( [0] => {:seg 'title' cache:} [1] => seg [2] => title [3] => cache ) ) Hope this starts you down the right path. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Reg-ex help
Craige Leeder schreef: > Hey guys, > > I'm trying to write a regular expression to match a tag for my > frameworks template engine. I seem to be having some trouble. The > expression should match: > > {:seg 'segname':} > {:seg 'segname' cache:} > > What I have is... > > $fSegRegEx = "#\{:seg \'[a-z0-9\-\_]{3,}\'( cache)?:\}#i"; hth: {:seg 'title':} {:seg 'title' cache:} DATA; preg_match_all($fSegRegEx, $str, $faSegMatches, PREG_SET_ORDER); print_r($faSegMatches); ?> OUTPUTS: Array ( [0] => Array ( [0] => {:seg 'title':} [1] => {:seg [2] => title [3] => :} ) [1] => Array ( [0] => {:seg 'title' cache:} [1] => {:seg [2] => title [3] => cache:} ) ) > > Which when run against my test data, seems to match: > > "{:seg 'segname' cache:}" > " cache" > > > > > > For arguments sake, I'll provide the whole code snippet... > >preg_match($fSegRegEx, $this->msOpCont, $faSegMatches); > >/* faSegMatches == > > * array > > * 0 => '{:seg 'users_online' cache:}'// > > * 1 => ' cache'// > > */ > > while ( $fMatch = current($faSegMatches) ) { > >/* > * Expected: > > * $fMatch[0] = "{:seg " > > * $fMatch[1] = Segment Name > > * > * $fMatch[2] = " cache:}" > > * or > > * $fMatch[2] = " :}" > > */ > >$faMatch = explode("'", $fMatch); > >//... > >//... > >next($faSegMatches); > > }// End While > > > Thanks guys. This has been bugging me for a couple days. > - Craige > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Reg-ex help
Craige Leeder wrote: Hey guys, I'm trying to write a regular expression to match a tag for my frameworks template engine. I seem to be having some trouble. The expression should match: {:seg 'segname':} {:seg 'segname' cache:} What I have is... $fSegRegEx = "#\{:seg \'[a-z0-9\-\_]{3,}\'( cache)?:\}#i"; Which when run against my test data, seems to match: "{:seg 'segname' cache:}" " cache" Thanks guys. This has been bugging me for a couple days. - Craige I used some of your code, but try this variant on for size. {:seg 'title':} {:seg 'title' cache:} DATA; # For arguments sake, I'll provide the whole code snippet... $fSegRegEx = "|\{:(seg) \'([a-z0-9\-\_]{3,})\'\s?(cache)?:\}|i"; preg_match_all($fSegRegEx, $dummyData, $faSegMatches, PREG_SET_ORDER); print_r($faSegMatches); ?> Not sure what your input is going to be (HTML, XML, etc...) so I used HTML Anyways, output from the above code is this: Array ( [0] => Array ( [0] => {:seg 'title':} [1] => seg [2] => title ) [1] => Array ( [0] => {:seg 'title' cache:} [1] => seg [2] => title [3] => cache ) ) Hope this starts you down the right path. -- Jim Lucas "Some men are born to greatness, some achieve greatness, and some have greatness thrust upon them." Twelfth Night, Act II, Scene V by William Shakespeare -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Reg-ex help
Hey guys, I'm trying to write a regular expression to match a tag for my frameworks template engine. I seem to be having some trouble. The expression should match: {:seg 'segname':} {:seg 'segname' cache:} What I have is... $fSegRegEx = "#\{:seg \'[a-z0-9\-\_]{3,}\'( cache)?:\}#i"; Which when run against my test data, seems to match: "{:seg 'segname' cache:}" " cache" For arguments sake, I'll provide the whole code snippet... preg_match($fSegRegEx, $this->msOpCont, $faSegMatches); /* faSegMatches == * array * 0 => '{:seg 'users_online' cache:}'// * 1 => ' cache'// */ while ( $fMatch = current($faSegMatches) ) { /* * Expected: * $fMatch[0] = "{:seg " * $fMatch[1] = Segment Name * * $fMatch[2] = " cache:}" * or * $fMatch[2] = " :}" */ $faMatch = explode("'", $fMatch); //... //... next($faSegMatches); }// End While Thanks guys. This has been bugging me for a couple days. - Craige -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] reg ex help
On Fri, November 4, 2005 12:09 pm, conditional motion wrote: > New to php so please bear with me. I'm trying to parse through a > field that has some information in it, the information is stored with > other information and is delimited in a certain pattern so I figure > using reg ex I can get the information I want out of the text. > > So here is an example. > > Silver Small Corp;X^%\n#\n > Gold Medium Corp;RE^%\n#\n > Platinum Large Corp;YRE^%\n#\n > > Reall all I need is the information on Silver Small Corp, etc and the > X all the rest is gibberish. Is Reg Ex the best way to do this or > would it be some other way. Try this (untested): The 'm' at the end might need to be an 's', but I think 'm' is right. -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] reg ex help
On Fri, 04 Nov 2005 14:09:06 -0500, conditional motion wrote: > Here is what the field content from the database would look like. I have > removed any sensitive data. > > str = "Name;Grill Transom (GT302)^% - > Sort;Find/Replace^% - Calc;2568.09x^% - > Type;Veck^% - PO Number;^% - Previous Order > Number;^% - Fabric Whole;Dyna-Dry^% - Fabric > ... If you really want a regex.. something like: preg_match_all('/\s*([^;]+);{5}([^^]*)^/U', $str, $matches); var_dump($matches); And you'll have an array of field => values (excuding the Coords field). I do wonder why this is stored in the db like this. Curt. -- http://news.zirzow.dyndns.org/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] reg ex help
explode on this first: '^% -' then probably on explode ';' like Jay mentioned. then you can probably massage the really weird stuff into shape somehow, stuff like (wtf :-)): Back Number Font;%Athletic^Athletic ..hth conditional motion wrote: The name like "Roch Small Sum" and others dont change, they will be in there whether there is a value associated with them or not. On 11/4/05, conditional motion <[EMAIL PROTECTED]> wrote: Here is what the field content from the database would look like. I have removed any sensitive data. str = "Name;Grill Transom (GT302)^% - Sort;Find/Replace^% - Calc;2568.09x^% - Type;Veck^% - PO Number;^% - Previous Order Number;^% - Fabric Whole;Dyna-Dry^% - Fabric Body;^% - Fabric Yoke;^% - Fabric Sleeves;^% - Fabric Panels;^% - ID;32398^%^%^%^%^% - Size;6.0^%^%^%^%^% - Layer;0^%^%^%^%^% - Coords;147^173^400^%^^^%^^^%^^^%^^^% - ID;%32400^%^%%%^% - Coords;%147^110^400^%^^^%%%^^^% - Back Number Font;%Athletic^Athletic^% - Front Text ID;%%32402^%32403^%^%^%^%^%^% - Roch Small Sum;0^% - Cards Med Extra;0^% - Roch Large Sum;0^% - Silver Small Corp;X^% - Gold Medium Corp;RE^% - Platinum Large Corp;YRE^%"; That is one field. On 11/4/05, Pablo Gosse <[EMAIL PROTECTED]> wrote: [snip] The problem with that is there are about 40 different listings in the one field. Silver Small Corp;X^%\n#\n Gold Medium Corp;RE^%\n#\n Platinum Large Corp;YRE^%\n#\n being three of them so maybe this is a bettter way of listing it ... Silver Small Corp;X^%\n#\nGold Medium Corp;RE^%\n#\nPlatinum Large Corp;YRE^%\n#\n ...[/snip] Try this: $values = array(); // we'll put the extracted vars here $str = "This is supposed to be your string"; $tmpStr = explode("#\n", $str); foreach ($tmpStr as $foo) { if (strlen(trim($foo)) > 0) { array_push($values, substr($foo, 0, strpos($foo, ';'))); } } HTH, Pablo -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] reg ex help
Whos responsible for putting all that information into one field. LOL On 11/4/05, conditional motion <[EMAIL PROTECTED]> wrote: > The name like "Roch Small Sum" and others dont change, they will be in > there whether there is a value associated with them or not. > > > On 11/4/05, conditional motion <[EMAIL PROTECTED]> wrote: > > Here is what the field content from the database would look like. I > > have removed any sensitive data. > > > > str = "Name;Grill Transom (GT302)^% - > > Sort;Find/Replace^% - Calc;2568.09x^% - > > Type;Veck^% - PO Number;^% - Previous Order > > Number;^% - Fabric Whole;Dyna-Dry^% - Fabric > > Body;^% - Fabric Yoke;^% - Fabric > > Sleeves;^% - Fabric Panels;^% - > > ID;32398^%^%^%^%^% - > > Size;6.0^%^%^%^%^% - > > Layer;0^%^%^%^%^% - > > Coords;147^173^400^%^^^%^^^%^^^%^^^% > > - ID;%32400^%^%%%^% > > - > > Coords;%147^110^400^%^^^%%%^^^% > > - Back Number > > Font;%Athletic^Athletic^% > > - Front Text > > ID;%%32402^%32403^%^%^%^%^%^% > > - Roch Small Sum;0^% - Cards Med Extra;0^% > > - Roch Large Sum;0^% - Silver Small Corp;X^% > > - Gold Medium Corp;RE^% - Platinum Large > > Corp;YRE^%"; > > > > That is one field. > > > > On 11/4/05, Pablo Gosse <[EMAIL PROTECTED]> wrote: > > > [snip] > > > The problem with that is there are about 40 different listings in the > > > one field. > > > > > > Silver Small Corp;X^%\n#\n > > > Gold Medium Corp;RE^%\n#\n > > > Platinum Large Corp;YRE^%\n#\n > > > > > > being three of them so maybe this is a bettter way of listing it > > > > > > ... Silver Small Corp;X^%\n#\nGold Medium > > > Corp;RE^%\n#\nPlatinum Large Corp;YRE^%\n#\n > > > ...[/snip] > > > > > > Try this: > > > > > > $values = array(); // we'll put the extracted vars here > > > $str = "This is supposed to be your string"; > > > > > > $tmpStr = explode("#\n", $str); > > > > > > foreach ($tmpStr as $foo) { > > >if (strlen(trim($foo)) > 0) { > > >array_push($values, substr($foo, 0, strpos($foo, ';'))); > > > > > >} > > > } > > > > > > HTH, > > > > > > Pablo > > > > > > -- > > > PHP General Mailing List (http://www.php.net/) > > > To unsubscribe, visit: http://www.php.net/unsub.php > > > > > > > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] reg ex help
The name like "Roch Small Sum" and others dont change, they will be in there whether there is a value associated with them or not. On 11/4/05, conditional motion <[EMAIL PROTECTED]> wrote: > Here is what the field content from the database would look like. I > have removed any sensitive data. > > str = "Name;Grill Transom (GT302)^% - > Sort;Find/Replace^% - Calc;2568.09x^% - > Type;Veck^% - PO Number;^% - Previous Order > Number;^% - Fabric Whole;Dyna-Dry^% - Fabric > Body;^% - Fabric Yoke;^% - Fabric > Sleeves;^% - Fabric Panels;^% - > ID;32398^%^%^%^%^% - > Size;6.0^%^%^%^%^% - > Layer;0^%^%^%^%^% - > Coords;147^173^400^%^^^%^^^%^^^%^^^% > - ID;%32400^%^%%%^% > - > Coords;%147^110^400^%^^^%%%^^^% > - Back Number > Font;%Athletic^Athletic^% > - Front Text > ID;%%32402^%32403^%^%^%^%^%^% > - Roch Small Sum;0^% - Cards Med Extra;0^% > - Roch Large Sum;0^% - Silver Small Corp;X^% > - Gold Medium Corp;RE^% - Platinum Large > Corp;YRE^%"; > > That is one field. > > On 11/4/05, Pablo Gosse <[EMAIL PROTECTED]> wrote: > > [snip] > > The problem with that is there are about 40 different listings in the > > one field. > > > > Silver Small Corp;X^%\n#\n > > Gold Medium Corp;RE^%\n#\n > > Platinum Large Corp;YRE^%\n#\n > > > > being three of them so maybe this is a bettter way of listing it > > > > ... Silver Small Corp;X^%\n#\nGold Medium > > Corp;RE^%\n#\nPlatinum Large Corp;YRE^%\n#\n > > ...[/snip] > > > > Try this: > > > > $values = array(); // we'll put the extracted vars here > > $str = "This is supposed to be your string"; > > > > $tmpStr = explode("#\n", $str); > > > > foreach ($tmpStr as $foo) { > >if (strlen(trim($foo)) > 0) { > >array_push($values, substr($foo, 0, strpos($foo, ';'))); > > > >} > > } > > > > HTH, > > > > Pablo > > > > -- > > PHP General Mailing List (http://www.php.net/) > > To unsubscribe, visit: http://www.php.net/unsub.php > > > > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] reg ex help
Here is what the field content from the database would look like. I have removed any sensitive data. str = "Name;Grill Transom (GT302)^% - Sort;Find/Replace^% - Calc;2568.09x^% - Type;Veck^% - PO Number;^% - Previous Order Number;^% - Fabric Whole;Dyna-Dry^% - Fabric Body;^% - Fabric Yoke;^% - Fabric Sleeves;^% - Fabric Panels;^% - ID;32398^%^%^%^%^% - Size;6.0^%^%^%^%^% - Layer;0^%^%^%^%^% - Coords;147^173^400^%^^^%^^^%^^^%^^^% - ID;%32400^%^%%%^% - Coords;%147^110^400^%^^^%%%^^^% - Back Number Font;%Athletic^Athletic^% - Front Text ID;%%32402^%32403^%^%^%^%^%^% - Roch Small Sum;0^% - Cards Med Extra;0^% - Roch Large Sum;0^% - Silver Small Corp;X^% - Gold Medium Corp;RE^% - Platinum Large Corp;YRE^%"; That is one field. On 11/4/05, Pablo Gosse <[EMAIL PROTECTED]> wrote: > [snip] > The problem with that is there are about 40 different listings in the > one field. > > Silver Small Corp;X^%\n#\n > Gold Medium Corp;RE^%\n#\n > Platinum Large Corp;YRE^%\n#\n > > being three of them so maybe this is a bettter way of listing it > > ... Silver Small Corp;X^%\n#\nGold Medium > Corp;RE^%\n#\nPlatinum Large Corp;YRE^%\n#\n > ...[/snip] > > Try this: > > $values = array(); // we'll put the extracted vars here > $str = "This is supposed to be your string"; > > $tmpStr = explode("#\n", $str); > > foreach ($tmpStr as $foo) { >if (strlen(trim($foo)) > 0) { >array_push($values, substr($foo, 0, strpos($foo, ';'))); > >} > } > > HTH, > > Pablo > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] reg ex help
[snip] The problem with that is there are about 40 different listings in the one field. Silver Small Corp;X^%\n#\n Gold Medium Corp;RE^%\n#\n Platinum Large Corp;YRE^%\n#\n being three of them so maybe this is a bettter way of listing it ... Silver Small Corp;X^%\n#\nGold Medium Corp;RE^%\n#\nPlatinum Large Corp;YRE^%\n#\n ...[/snip] Try this: $values = array(); // we'll put the extracted vars here $str = "This is supposed to be your string"; $tmpStr = explode("#\n", $str); foreach ($tmpStr as $foo) { if (strlen(trim($foo)) > 0) { array_push($values, substr($foo, 0, strpos($foo, ';'))); } } HTH, Pablo -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] reg ex help
[snip] New to php so please bear with me. I'm trying to parse through a field that has some information in it, the information is stored with other information and is delimited in a certain pattern so I figure using reg ex I can get the information I want out of the text. So here is an example. Silver Small Corp;X^%\n#\n Gold Medium Corp;RE^%\n#\n Platinum Large Corp;YRE^%\n#\n Reall all I need is the information on Silver Small Corp, etc and the X all the rest is gibberish. Is Reg Ex the best way to do this or would it be some other way.[/snip] If the number of semi-colons is fixed, then explode will do the trick. $foo = 'Silver Small Corp;X^%\n#\n'; $bar = explode(';', $foo); echo $bar[0]; However if the number of semi-colons is not fixed, then substr in conjunction with strpos will do the trick. $foo = 'Silver Small Corp;X^%\n#\n'; $bar = substr($foo, 0, strpos($foo, ';')); echo $bar; HTH, Pablo -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] reg ex help
The problem with that is there are about 40 different listings in the one field. Silver Small Corp;X^%\n#\n Gold Medium Corp;RE^%\n#\n Platinum Large Corp;YRE^%\n#\n being three of them so maybe this is a bettter way of listing it ... Silver Small Corp;X^%\n#\nGold Medium Corp;RE^%\n#\nPlatinum Large Corp;YRE^%\n#\n ... On 11/4/05, Jay Blanchard <[EMAIL PROTECTED]> wrote: > [snip] > New to php so please bear with me. I'm trying to parse through a > field that has some information in it, the information is stored with > other information and is delimited in a certain pattern so I figure > using reg ex I can get the information I want out of the text. > > So here is an example. > > Silver Small Corp;X^%\n#\n > Gold Medium Corp;RE^%\n#\n > Platinum Large Corp;YRE^%\n#\n > > Reall all I need is the information on Silver Small Corp, etc and the > X all the rest is gibberish. Is Reg Ex the best way to do this or > would it be some other way. > [/snip] > > You couuld use explode > > $myField = "Silver Small Corp;X^%\n#\n"; > > $foo = explode(";", $myField); > > echo $foo[0]; > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] reg ex help
[snip] New to php so please bear with me. I'm trying to parse through a field that has some information in it, the information is stored with other information and is delimited in a certain pattern so I figure using reg ex I can get the information I want out of the text. So here is an example. Silver Small Corp;X^%\n#\n Gold Medium Corp;RE^%\n#\n Platinum Large Corp;YRE^%\n#\n Reall all I need is the information on Silver Small Corp, etc and the X all the rest is gibberish. Is Reg Ex the best way to do this or would it be some other way. [/snip] You couuld use explode $myField = "Silver Small Corp;X^%\n#\n"; $foo = explode(";", $myField); echo $foo[0]; -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] reg ex help
New to php so please bear with me. I'm trying to parse through a field that has some information in it, the information is stored with other information and is delimited in a certain pattern so I figure using reg ex I can get the information I want out of the text. So here is an example. Silver Small Corp;X^%\n#\n Gold Medium Corp;RE^%\n#\n Platinum Large Corp;YRE^%\n#\n Reall all I need is the information on Silver Small Corp, etc and the X all the rest is gibberish. Is Reg Ex the best way to do this or would it be some other way. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
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 HTMLoutput
One way to do it is: $NewString = ereg_replace('[[:space:]]+', ' ', $String); There are also ways to do it with preg functions that are slightly more efficient; see the pcre docs. And, the standard (AFAIK) reference book for regular expressions is O'Reilly's 'Mastering Regular Expressions'; a worthwhile purchase. -steve At 2:06 AM -0500 12/5/01, Ken wrote: >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] > -- ++ | Steve Edberg [EMAIL PROTECTED] | | University of California, Davis (530)754-9127 | | Programming/Database/SysAdmin http://pgfsun.ucdavis.edu/ | ++ | "Restriction of free thought and free speech is the most dangerous of | | all subversions. It is the one un-American act that could most easily | | defeat us."| | - Supreme Court Justice (1939-1975) William O. Douglas | ++ -- 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]
[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]