Splitting a string

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 (...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 11-14-2006
Børge Holen
 
Posts: n/a
Default Splitting a string

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
---
Reply With Quote
  #2 (permalink)  
Old 11-15-2006
Darrell Brogdon
 
Posts: n/a
Default Re: [PHP] Splitting a string

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 **
*************************************



Reply With Quote
  #3 (permalink)  
Old 11-15-2006
Aaron Koning
 
Posts: n/a
Default Re: [PHP] Splitting a string

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/
+--------------------------------------------

Reply With Quote
  #4 (permalink)  
Old 11-15-2006
Graham Anderson
 
Posts: n/a
Default Inserting header() info into ob_start

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


Reply With Quote
  #5 (permalink)  
Old 11-15-2006
Paul Novitski
 
Posts: n/a
Default Re: [PHP] Splitting a string

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
Reply With Quote
  #6 (permalink)  
Old 11-15-2006
Robin Vickery
 
Posts: n/a
Default Re: [PHP] Splitting a string

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);
Reply With Quote
  #7 (permalink)  
Old 11-15-2006
Børge Holen
 
Posts: n/a
Default Re: [PHP] Splitting a string

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
---
Reply With Quote
  #8 (permalink)  
Old 11-15-2006
Børge Holen
 
Posts: n/a
Default Re: [PHP] Splitting a string

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
---
Reply With Quote
  #9 (permalink)  
Old 11-15-2006
Børge Holen
 
Posts: n/a
Default Re: [PHP] Splitting a string

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
---
Reply With Quote
  #10 (permalink)  
Old 11-15-2006
Chris
 
Posts: n/a
Default Re: [PHP] Splitting a string

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/
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 02:55 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0