This is a discussion on Splitting a string within the PHP General forums, part of the PHP Programming Forums category; This numer has dynamic lenght, witch is the root of my problems. $number = 123456789 should print as following: var1: 12345 (...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
This numer has dynamic lenght, witch is the root of my problems.
$number = 123456789 should print as following: var1: 12345 (and it is this lengt witch varies) var2: 67 var3: 89. I've been using substr with negative numbers to fetch the last two vars. thereafter explode to get the first number. Ok I understand the code and can use it, but it feels wrong. Does anyone have any idea on suitable functions for such a task, both more size (as in code) and speed effective. It is not a mission critical, but moreover a opinion of what things should look like. I've been looking into string/explode functions on php.net without getting past my current code. -- --- Børge Kennel Arivene http://www.arivene.net --- |
|
|||
|
What's the code?
-D On Nov 14, 2006, at 4:17 PM, Børge Holen wrote: > This numer has dynamic lenght, witch is the root of my problems. > > $number = 123456789 > > should print as following: > var1: 12345 (and it is this lengt witch varies) > var2: 67 > var3: 89. > > I've been using substr with negative numbers to fetch the last two > vars. > thereafter explode to get the first number. > Ok I understand the code and can use it, but it feels wrong. > Does anyone have any idea on suitable functions for such a task, > both more > size (as in code) and speed effective. It is not a mission > critical, but > moreover a opinion of what things should look like. > I've been looking into string/explode functions on php.net without > getting > past my current code. > > > -- > --- > Børge > Kennel Arivene > http://www.arivene.net > --- > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > Darrell Brogdon darrell@brogdon.net http://darrell.brogdon.net ************************************* ** Prepare for PHP Certication! ** ** http://phpflashcards.com ** ************************************* |
|
|||
|
Assuming var1 and var2 only ever use the last four numbers (untested):
$length = strlen($number); // get string length $var1 = substr($number,0,$length-4); // get number until only 4 numbers are left $var2 = substr($number,$length-4,2); // get 3rd and 4th last numbers. $var3 = substr($number,$length-2,2); // get last two numbers. Aaron On 11/14/06, Børge Holen <borge@arivene.net> wrote: > > This numer has dynamic lenght, witch is the root of my problems. > > $number = 123456789 > > should print as following: > var1: 12345 (and it is this lengt witch varies) > var2: 67 > var3: 89. > > I've been using substr with negative numbers to fetch the last two vars. > thereafter explode to get the first number. > Ok I understand the code and can use it, but it feels wrong. > Does anyone have any idea on suitable functions for such a task, both more > size (as in code) and speed effective. It is not a mission critical, but > moreover a opinion of what things should look like. > I've been looking into string/explode functions on php.net without getting > past my current code. > > > -- > --- > Børge > Kennel Arivene > http://www.arivene.net > --- > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- +-------------------------------------------- | Aaron Koning | Information Technologist | Prince George, BC, Canada. +-------------------------------------------- | http://datashare.gis.unbc.ca/fist/ | http://datashare.gis.unbc.ca/gctp-js/ +-------------------------------------------- |
|
|||
|
How can I insert header() info directly into the ob_start stream?
I am compressing a css file with php before it is outputted to the browser, The below DOES work, but I have to insert the php header() info DIRECTLY into the css file. Otherwise, mystyle.css is interpreted as text/html instead of text/css. If possible, I would like to keep css and php code separated. Is this possible, or is there an alternate way? many thanks compress-css.php: <?php /* header info is not working anywhere I put it :( */ /* header('content-type:text/css');*/ header("Expires: ".gmdate("D, d M Y H:i:s", (time()+900)) . " GMT"); */ ob_start("compress"); function compress($buffer) { // remove comments $buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer); // remove tabs, spaces, newlines, etc. $buffer = str_replace(array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), '', $buffer); return $buffer; } ?> mystyle.css: /* css file is run thru compress-css.php before outputed to the browser */ <?php header('content-type:text/css'); header("Expires: ".gmdate("D, d M Y H:i:s", (time()+900)) . " GMT"); ?> #menu {font: normal 1.88em/1em Georgia, Times New Roman, Times, serif; font-variant: all-caps; margin:0 .1em;} ....rest of css goes here ..htaccess file: AddHandler application/x-httpd-php .css php_value auto_prepend_file /path/to/compress-css.php |
|
|||
|
At 11/14/2006 03:17 PM, Børge Holen wrote:
>$number = 123456789 > >should print as following: >var1: 12345 (and it is this lengt witch varies) >var2: 67 >var3: 89. You can also do this with a regular expression: $iNumber = '123456789'; $sPattern = '/(\d+)(\d{2})(\d{2})$/'; preg_match($sPattern, $iNumber, $aMatches); Then $aMatches contains: Array ( [0] => 123456789 [1] => 12345 [2] => 67 [3] => 89 ) The regexp pattern /(\d+)(\d{2})(\d{2})$/ means: (one or more digits) (two digits) (two digits) [end of string] preg_match http://ca3.php.net/manual/en/function.preg-match.php Pattern Syntax http://ca3.php.net/manual/en/referen...ern.syntax.php Regards, Paul |
|
|||
|
On 15/11/06, Aaron Koning <aaronkoning@gmail.com> wrote:
> Assuming var1 and var2 only ever use the last four numbers (untested): > > $length = strlen($number); // get string length > $var1 = substr($number,0,$length-4); // get number until only 4 numbers are > left > $var2 = substr($number,$length-4,2); // get 3rd and 4th last numbers. > $var3 = substr($number,$length-2,2); // get last two numbers. > No need to work out the length of the string. $var1 = substr($number, 0, -4); $var2 = substr($number, -4, 2); $var3 = substr($number, -2); |
|
|||
|
On Wednesday 15 November 2006 06:24, you wrote:
> At 11/14/2006 03:17 PM, Børge Holen wrote: > >$number = 123456789 > > > >should print as following: > >var1: 12345 (and it is this lengt witch varies) > >var2: 67 > >var3: 89. > > You can also do this with a regular expression: > > $iNumber = '123456789'; > $sPattern = '/(\d+)(\d{2})(\d{2})$/'; > preg_match($sPattern, $iNumber, $aMatches); > > Then $aMatches contains: > > Array > ( > [0] => 123456789 > [1] => 12345 > [2] => 67 > [3] => 89 > ) > > The regexp pattern /(\d+)(\d{2})(\d{2})$/ means: > > (one or more digits) (two digits) (two digits) [end of string] > > preg_match > http://ca3.php.net/manual/en/function.preg-match.php > > Pattern Syntax > http://ca3.php.net/manual/en/referen...ern.syntax.php > > Regards, > Paul Thanks. Its a much more sophisticated code with three values in one array, rather than my solution with 2 vars from the substr and one array with one value from explode. Never used preg_match like this before. =) -- --- Børge Kennel Arivene http://www.arivene.net --- |
|
|||
|
On Wednesday 15 November 2006 12:42, Robin Vickery wrote:
> On 15/11/06, Aaron Koning <aaronkoning@gmail.com> wrote: > > Assuming var1 and var2 only ever use the last four numbers (untested): > > > > $length = strlen($number); // get string length > > $var1 = substr($number,0,$length-4); // get number until only 4 numbers > > are left > > $var2 = substr($number,$length-4,2); // get 3rd and 4th last numbers. > > $var3 = substr($number,$length-2,2); // get last two numbers. > > No need to work out the length of the string. > > $var1 = substr($number, 0, -4); > $var2 = substr($number, -4, 2); > $var3 = substr($number, -2); Why do I suddenly feel stupid ;D magnificent! Easier than the preg_match solution, but not as compact. -- --- Børge Kennel Arivene http://www.arivene.net --- |
|
|||
|
Oh this was good.
I added a while loop to insert extra strings "0" in front of the number to add if the string is less than 5 chars short. I forgot to mentinon that the string actually could be shorter (just found out) and the code didn't work with fewer than 5 char strings. But now is rocks. Thanks again. On Wednesday 15 November 2006 06:24, Paul Novitski wrote: > At 11/14/2006 03:17 PM, Børge Holen wrote: > >$number = 123456789 > > > >should print as following: > >var1: 12345 (and it is this lengt witch varies) > >var2: 67 > >var3: 89. > > You can also do this with a regular expression: > > $iNumber = '123456789'; > $sPattern = '/(\d+)(\d{2})(\d{2})$/'; > preg_match($sPattern, $iNumber, $aMatches); > > Then $aMatches contains: > > Array > ( > [0] => 123456789 > [1] => 12345 > [2] => 67 > [3] => 89 > ) > > The regexp pattern /(\d+)(\d{2})(\d{2})$/ means: > > (one or more digits) (two digits) (two digits) [end of string] > > preg_match > http://ca3.php.net/manual/en/function.preg-match.php > > Pattern Syntax > http://ca3.php.net/manual/en/referen...ern.syntax.php > > Regards, > Paul -- --- Børge Kennel Arivene http://www.arivene.net --- |
|
|||
|
Børge Holen wrote:
> Oh this was good. > I added a while loop to insert extra strings "0" in front of the number to add > if the string is less than 5 chars short. sprintf is your friend here, no need to use a loop. sprintf('%05d', '1234'); -- Postgresql & php tutorials http://www.designmagick.com/ |
![]() |
| Thread Tools | |
| Display Modes | |
|
|