Re: [PHP] Splitting a string ...

2010-03-15 Thread shiplu
Here is the regex for you.


$company_domain = '\w+'; // replace with your own company domain pattern.
$user_name = '\w+'; // replace with your own username pattern
$email_domain = '\w+\.\w{2,4}'; // google for standard domain name
regex pattern and replace it.

$regexp = 
~({$company_domain}[/])?(?Pusername$user_name)(@$email_domain)?~;

preg_match($regexp, $text, $matches);

print_r($matches); // $matches['username'] will contain username.

-- 
Shiplu Mokaddim
My talks, http://talk.cmyweb.net
Follow me, http://twitter.com/shiplu
SUST Programmers, http://groups.google.com/group/p2psust
Innovation distinguishes bet ... ... (ask Steve Jobs the rest)

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Splitting a string ...

2010-03-14 Thread Ashley M. Kirchner
I'm not a regexp person (wish I was though), and I'm hoping someone can give
me a hand here.  Consider the following strings:

 

-  domain\usern...@example.org

-  domain\username

-  the same as above but with / instead of \  (hey, it happens)

-  usern...@example.org

-  username

 

Essentially I have a sign-up form where folks will be typing in their
username.  The problem is, in our organization, when you tell someone to
enter their username, it could end up being any of the above examples
because they're used to a domain log in procedure where in some cases they
type the whole thing, in other cases just the e-mail, or sometimes just the
username.

 

So what I'd like is a way to capture just the 'username' part, regardless of
what other pieces they put in.  In the past I would write a rather
inefficient split() routine and eventually get what I need.  With split()
getting deprecated, I figured I may as well start looking into how to do it
properly.  There's preg_split(), str_split(), explode() . possibly others.

 

So, what's the proper way to do this?  How can I capture just the part I
need, regardless of how they typed it in?

 

Thanks!

 

A



Re: [PHP] Splitting a string ...

2010-03-14 Thread Jochem Maas
Op 3/15/10 1:54 AM, Ashley M. Kirchner schreef:
 I'm not a regexp person (wish I was though), and I'm hoping someone can give
 me a hand here.  Consider the following strings:
 
  
 
 -  domain\usern...@example.org
 
 -  domain\username
 
 -  the same as above but with / instead of \  (hey, it happens)
 
 -  usern...@example.org
 
 -  username
 
  
 
 Essentially I have a sign-up form where folks will be typing in their
 username.  The problem is, in our organization, when you tell someone to
 enter their username, it could end up being any of the above examples
 because they're used to a domain log in procedure where in some cases they
 type the whole thing, in other cases just the e-mail, or sometimes just the
 username.
 
  
 
 So what I'd like is a way to capture just the 'username' part, regardless of
 what other pieces they put in.  In the past I would write a rather
 inefficient split() routine and eventually get what I need.  With split()
 getting deprecated, I figured I may as well start looking into how to do it
 properly.  There's preg_split(), str_split(), explode() . possibly others.
 
  
 
 So, what's the proper way to do this?  How can I capture just the part I
 need, regardless of how they typed it in?
 

?php

$inputs = array(
// double slashes just due to escaping in literal strings
domain\\usern...@example.org,
domain\\username,
domain/usern...@example.org,
domain/username,
usern...@example.org,
username,
);
foreach ($inputs as $input) {
// four slashes = two slashes in the actual regexp!
preg_match(#^(?:.*[/])?([...@]*)(?:@.*)?$#, $input, $match);
var_dump($match[1]);
}

?

... just off the top of my head, probably could be done better than this.
I would recommend reverse engineering the given regexp to find out what
it's doing exactly ... painstaking but worth it to go through it char by char,
it might be the start of a glorious regexp career :)

  
 
 Thanks!
 
  
 
 A
 
 


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] splitting a string

2010-01-05 Thread Ingleby, Les
Hi all, first time I have posted here so please be nice.

I am using PEAR HTTP_Upload to handle multiple file uploads. What I need to do 
is to take the file name which is output using the getProp() function and then 
remove the file extension from the end of the file for example:

Original name held in the getProp() array [name] word_doccument.docx

I need to put this into a string such as

$filename = $props['name'];

I then need to separate the file name from the file extension.

I have been reading the php manual do please don't bother referring me to that.

Thanks in advance.

LEGAL INFORMATION
Information contained in this e-mail may be subject to public disclosure under 
the Freedom of Information Act 2000. Unless the information is legally exempt, 
the confidentiality of this e-mail and your reply cannot be guaranteed. 
Unless expressly stated otherwise, the information contained in this e-mail  
any files transmitted with it are intended for the recipient only. If you are 
not the intended recipient you must not copy, distribute, or take any action or 
reliance upon it. If you have received this e-mail in error, you should notify 
the sender immediately and delete this email. Any unauthorised disclosure of 
the information contained in this e-mail is strictly prohibited.  Any views or 
opinions presented are solely those of the author and do not necessarily 
represent those of Tyne Metropolitan College unless explicitly stated otherwise.
This e-mail and attachments have been scanned for viruses prior to leaving Tyne 
Metropolitan College. Tyne Metropolitan College will not be liable for any 
losses as a result of any viruses being passed on.


Re: [PHP] splitting a string

2010-01-05 Thread Daniel Egeberg
On Tue, Jan 5, 2010 at 13:39, Ingleby, Les les.in...@tynemet.ac.uk wrote:
 Hi all, first time I have posted here so please be nice.

 I am using PEAR HTTP_Upload to handle multiple file uploads. What I need to 
 do is to take the file name which is output using the getProp() function and 
 then remove the file extension from the end of the file for example:

 Original name held in the getProp() array [name] word_doccument.docx

 I need to put this into a string such as

 $filename = $props['name'];

 I then need to separate the file name from the file extension.

 I have been reading the php manual do please don't bother referring me to 
 that.

 Thanks in advance.

There are a couple of ways you could do this. The most straightforward
way would be to use the pathinfo() function.

?php
$file = 'word_document.docx';
$extension = pathinfo($file, PATHINFO_EXTENSION);
$name = pathinfo($file, PATHINFO_FILENAME);
?

See http://php.net/pathinfo for more information about that function.

You could also explode() it and do something with that, or you could
or use some of the other many string manipulation functions. I think
pathinfo() is easiest.

-- 
Daniel Egeberg

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] splitting a string

2010-01-05 Thread Ashley Sheridan
On Tue, 2010-01-05 at 12:39 +, Ingleby, Les wrote:

 Hi all, first time I have posted here so please be nice.
 
 I am using PEAR HTTP_Upload to handle multiple file uploads. What I need to 
 do is to take the file name which is output using the getProp() function and 
 then remove the file extension from the end of the file for example:
 
 Original name held in the getProp() array [name] word_doccument.docx
 
 I need to put this into a string such as
 
 $filename = $props['name'];
 
 I then need to separate the file name from the file extension.
 
 I have been reading the php manual do please don't bother referring me to 
 that.
 
 Thanks in advance.
 
 LEGAL INFORMATION
 Information contained in this e-mail may be subject to public disclosure 
 under the Freedom of Information Act 2000. Unless the information is legally 
 exempt, the confidentiality of this e-mail and your reply cannot be 
 guaranteed. 
 Unless expressly stated otherwise, the information contained in this e-mail  
 any files transmitted with it are intended for the recipient only. If you are 
 not the intended recipient you must not copy, distribute, or take any action 
 or reliance upon it. If you have received this e-mail in error, you should 
 notify the sender immediately and delete this email. Any unauthorised 
 disclosure of the information contained in this e-mail is strictly 
 prohibited.  Any views or opinions presented are solely those of the author 
 and do not necessarily represent those of Tyne Metropolitan College unless 
 explicitly stated otherwise.
 This e-mail and attachments have been scanned for viruses prior to leaving 
 Tyne Metropolitan College. Tyne Metropolitan College will not be liable for 
 any losses as a result of any viruses being passed on.


PHPBB uses the substr() function along with the strrpos() function to
grab the extension from a file. You could do something like this:
(untested - I always forget the order of the params!)

$name = substr($filename, 0, strrpos($filename, '.'));

Thanks,
Ash
http://www.ashleysheridan.co.uk




Re: [PHP] splitting a string

2010-01-05 Thread Daniel Egeberg
On Tue, Jan 5, 2010 at 14:13, Ashley Sheridan a...@ashleysheridan.co.uk wrote:
 (untested - I always forget the order of the params!)

As a general rule, string functions are always haystack-needle and
array functions are always needle-haystack. I can't think of any
exceptions to that rule.

-- 
Daniel Egeberg

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] splitting a string

2010-01-05 Thread Ashley Sheridan
On Tue, 2010-01-05 at 14:26 +0100, Daniel Egeberg wrote:

 On Tue, Jan 5, 2010 at 14:13, Ashley Sheridan a...@ashleysheridan.co.uk 
 wrote:
  (untested - I always forget the order of the params!)
 
 As a general rule, string functions are always haystack-needle and
 array functions are always needle-haystack. I can't think of any
 exceptions to that rule.
 
 -- 
 Daniel Egeberg
 


Thanks! I'll try and remember that. It'll save me many trips to the
manual pages as well!

Thanks,
Ash
http://www.ashleysheridan.co.uk




Re: [PHP] Splitting a string

2006-11-16 Thread Børge Holen
On Thursday 16 November 2006 01:12, Robert Cummings wrote:
 On Thu, 2006-11-16 at 10:47 +1100, Chris wrote:
  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');

 No need to use a sledgehammer when a screwdriver will suffice:

 ?php

 echo str_pad( '1234', 5, '0', STR_PAD_LEFT )

Yes this is perfect. Thanks...
I repeat this step for about a few hundred values.
So this speedup is greatly appreciated.


 ?

 Cheers,
 Rob.
 --
 ..

 | InterJinn Application Framework - http://www.interjinn.com |
 |
 ::
 :
 | An application and templating framework for PHP. Boasting  |
 | a powerful, scalable system for accessing system services  |
 | such as forms, properties, sessions, and caches. InterJinn |
 | also provides an extremely flexible architecture for   |
 | creating re-usable components quickly and easily.  |

 `'

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



Re: [PHP] Splitting a string

2006-11-16 Thread Børge Holen
On Thursday 16 November 2006 01:38, Paul Novitski wrote:
 At 11/15/2006 02:06 PM, 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.
 
 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.

 Hey Børge,

 If you need to left-pad with zeroes, PHP comes to the rescue:
 http://php.net/str_pad

 However, if you're using the regular expression
 method then you might not need to pad the
 number.  You can change the pattern from this:

  /(\d+)(\d{2})(\d{2})$/'
 to this:
  /(\d*)(\d{2})(\d{2})$/'

 so it won't require any digits before the final two pairs.

  *   0 or more quantifier
  +   1 or more quantifier

 http://ca.php.net/manual/en/reference.pcre.pattern.syntax.php

 Paul

Cool solution, and it works.  =D
I do however need some chars to fill in on the finished product for the look 
of it all, so the 0 is needed... Witch is a bit of a shame with this cool 
string.

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



Re: [PHP] Splitting a string

2006-11-16 Thread Paul Novitski



On Thursday 16 November 2006 01:38, Paul Novitski wrote:
 If you need to left-pad with zeroes, PHP comes to the rescue:
 http://php.net/str_pad

 However, if you're using the regular expression
 method then you might not need to pad the
 number.  You can change the pattern from this:

  /(\d+)(\d{2})(\d{2})$/'
 to this:
  /(\d*)(\d{2})(\d{2})$/'


At 11/16/2006 03:23 PM, Børge Holen wrote:

Cool solution, and it works.  =D
I do however need some chars to fill in on the finished product for the look
of it all, so the 0 is needed... Witch is a bit of a shame with this cool
string.



Well, just to make sure you don't discard regexp unnecessarily...

// the pattern guarantees five digits, then two, then two:
$sPattern = '/(\d{5})(\d{2})(\d{2})$/';

// prepend 9 zeroes to the number to enforce the minimum requirements:
preg_match($sPattern, '0' . $iNumber, $aMatches);

Results:

$iNumber = '';
$aMatches:
(
[0] = 0
[1] = 0
[2] = 00
[3] = 00
)

$iNumber = '123';
$aMatches:
(
[0] = 00123
[1] = 0
[2] = 01
[3] = 23
)

$iNumber = '12345';
$aMatches:
(
[0] = 12345
[1] = 1
[2] = 23
[3] = 45
)

$iNumber = '123456789';
$aMatches:
(
[0] = 123456789
[1] = 12345
[2] = 67
[3] = 89
)


Paul 


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Splitting a string

2006-11-15 Thread Robin Vickery

On 15/11/06, Aaron Koning [EMAIL PROTECTED] 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);

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Splitting a string

2006-11-15 Thread Børge Holen
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/reference.pcre.pattern.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
---

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Splitting a string

2006-11-15 Thread Børge Holen
On Wednesday 15 November 2006 12:42, Robin Vickery wrote:
 On 15/11/06, Aaron Koning [EMAIL PROTECTED] 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
---

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Splitting a string

2006-11-15 Thread Børge Holen
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/reference.pcre.pattern.syntax.php

 Regards,
 Paul

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



Re: [PHP] Splitting a string

2006-11-15 Thread Chris

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/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Splitting a string

2006-11-15 Thread Robert Cummings
On Thu, 2006-11-16 at 10:47 +1100, Chris wrote:
 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');

No need to use a sledgehammer when a screwdriver will suffice:

?php

echo str_pad( '1234', 5, '0', STR_PAD_LEFT )

?

Cheers,
Rob.
-- 
..
| InterJinn Application Framework - http://www.interjinn.com |
::
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for   |
| creating re-usable components quickly and easily.  |
`'

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Splitting a string

2006-11-15 Thread Chris

Robert Cummings wrote:

On Thu, 2006-11-16 at 10:47 +1100, Chris wrote:

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');


No need to use a sledgehammer when a screwdriver will suffice:

?php

echo str_pad( '1234', 5, '0', STR_PAD_LEFT )

?


I knew there was another way ;)

Thanks for the reminder..

--
Postgresql  php tutorials
http://www.designmagick.com/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Splitting a string

2006-11-15 Thread Paul Novitski

At 11/15/2006 02:06 PM, 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.

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.



Hey Børge,

If you need to left-pad with zeroes, PHP comes to the rescue:
http://php.net/str_pad

However, if you're using the regular expression 
method then you might not need to pad the 
number.  You can change the pattern from this:


/(\d+)(\d{2})(\d{2})$/'
to this:
/(\d*)(\d{2})(\d{2})$/'

so it won't require any digits before the final two pairs.

*   0 or more quantifier
+   1 or more quantifier

http://ca.php.net/manual/en/reference.pcre.pattern.syntax.php

Paul

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Splitting a string

2006-11-14 Thread Børge Holen
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



Re: [PHP] Splitting a string

2006-11-14 Thread Darrell Brogdon

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
[EMAIL PROTECTED]
http://darrell.brogdon.net

*
** Prepare for PHP Certication!**
** http://phpflashcards.com**
*




Re: [PHP] Splitting a string

2006-11-14 Thread Aaron Koning

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 [EMAIL PROTECTED] 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/
+


Re: [PHP] Splitting a string

2006-11-14 Thread Paul Novitski

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/reference.pcre.pattern.syntax.php

Regards,
Paul 


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Splitting a string by the number of characters in the string?

2004-07-30 Thread Brent Clements
In PHP 5 there is a awesome function called str_split, is there an equivalent in PHP 
4.x?

I need to do the following:

Split a 60 character string into 3 20 character array chunks.

using str_split I could easily do it, but how do I do it in PHP 4.x?

Thanks,
Brent


Re: [PHP] Splitting a string by the number of characters in the string?

2004-07-30 Thread Justin Patrin
On Fri, 30 Jul 2004 02:44:19 -0500, Brent Clements
[EMAIL PROTECTED] wrote:
 In PHP 5 there is a awesome function called str_split, is there an equivalent in PHP 
 4.x?
 
 I need to do the following:
 
 Split a 60 character string into 3 20 character array chunks.
 
 using str_split I could easily do it, but how do I do it in PHP 4.x?
 

RTFM! Look at the See Alsos in the manual on the page for str_split.

-- 
DB_DataObject_FormBuilder - The database at your fingertips
http://pear.php.net/package/DB_DataObject_FormBuilder

paperCrane --Justin Patrin--

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Splitting a string by the number of characters in the string?

2004-07-30 Thread Jason Wong
On Friday 30 July 2004 15:44, Brent Clements wrote:
 In PHP 5 there is a awesome function called str_split, is there an
 equivalent in PHP 4.x?

 I need to do the following:

 Split a 60 character string into 3 20 character array chunks.

 using str_split I could easily do it, but how do I do it in PHP 4.x?

  substr()

-- 
Jason Wong - Gremlins Associates - www.gremlins.biz
Open Source Software Systems Integrators
* Web Design  Hosting * Internet  Intranet Applications Development *
--
Search the list archives before you post
http://marc.theaimsgroup.com/?l=php-general
--
/*
Superstition, idolatry, and hypocrisy have ample wages, but truth goes
a-begging.
-- Martin Luther
*/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Splitting a string

2003-03-13 Thread Christopher J. Crane
I have a CSV file that has 7 fields. One of the fields has a number, and
some of the numbers start with a S. If that number start with a S, I
want to strip it off. I am not sure how to do that. I first wrote the script
to open the file, load each line into an array and split the array by field.
Then split the variable where there is a S. The problem showed up when
there is another S in the field. I only want to split the first S at the
beginning of the field. Isn't there an additional value to add to the split
function that will only split by the qualifier once.

Here is what I have now.
?php
$OutputFile = fopen(2ndrun.txt, w) or die(Can't open File);
fwrite($OutputFile,
\Contract\,\PN\,\SN\,\Uin\,\Type\,\Descrip\,\Qty\,\Ship
Date\\n);

$fp = fopen (original.txt,r);
$Row = 1;
while($data = fgetcsv($fp, 1000, ,)) {
 if($Row != 1) {
  //Contract,PN,SN,Uin,Type,Descrip,Qty,Ship Date
  fwrite($OutputFile, \$data[0]\,\$data[1]\,);
 list($SN1, $SN2) = split(S, $data[2]);
  fwrite($OutputFile,
\$SN2\,\$data[3]\,\$data[4]\,\$data[5]\,\$data[6]\,\$data[7]\\n
);
  }
 $Row++;
 }
fclose ($fp);
fclose ($OutputFile);
print Done!\n;
?



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Splitting a string

2003-03-13 Thread - Edwin
Hi,

Christopher J. Crane [EMAIL PROTECTED] wrote: 

[snip]
 If that number start with a S, I want to strip it off.
[/snip]

Why don't you just check whether the first character is an S then 
return only the rest of the string if it is? Like:

?php
  $my_string = 'S12345';
  if ($my_string{0} == 'S'){
$rest_of_the_string = substr($my_string, 1);
  }
  echo $rest_of_the_string;
?

Of course, there could be some other way...

- E

__
Do You Yahoo!?
Yahoo! BB is Broadband by Yahoo!  http://bb.yahoo.co.jp/


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Splitting a string

2003-03-13 Thread David Otton
On Thu, 13 Mar 2003 09:13:35 -0500, you wrote:

Then split the variable where there is a S. The problem showed up when
there is another S in the field. I only want to split the first S at the
beginning of the field. Isn't there an additional value to add to the split

$line = 'S12345';
if ($line[0] == 'S') {
/* do stuff */
}


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Splitting a string

2003-03-13 Thread David Rice

Then split the variable where there is a S. The problem showed up 
when
there is another S in the field. I only want to split the first S 
at the
beginning of the field. Isn't there an additional value to add to the 
split
$line = 'S12345';
if ($line[0] == 'S') {
/* do stuff */
}
$str = S12345;
$str1 = ltrim($str,S);
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


RE: [PHP] Splitting a string

2003-03-13 Thread Crane, Christopher
This is perfect. I was not sure how to use the brackets with a variable for
position. Thank you very much

Christopher J. Crane 
Network Manager - Infrastructure Services 
IKON The Way Business Gets Communicated 
755 Winding Brook Drive
Glastonbury, CT 06078
Phone - (860) 659-6464
Fax - (860) 652-4379




-Original Message-
From: - Edwin [mailto:[EMAIL PROTECTED] 
Sent: Thursday, March 13, 2003 9:30 AM
To: Christopher J. Crane; [EMAIL PROTECTED]
Subject: Re: [PHP] Splitting a string


Hi,

Christopher J. Crane [EMAIL PROTECTED] wrote: 

[snip]
 If that number start with a S, I want to strip it off.
[/snip]

Why don't you just check whether the first character is an S then 
return only the rest of the string if it is? Like:

?php
  $my_string = 'S12345';
  if ($my_string{0} == 'S'){
$rest_of_the_string = substr($my_string, 1);
  }
  echo $rest_of_the_string;
?

Of course, there could be some other way...

- E

__
Do You Yahoo!?
Yahoo! BB is Broadband by Yahoo!  http://bb.yahoo.co.jp/


Re: [PHP] Splitting a string

2003-03-13 Thread - Edwin
Hi,

David Rice [EMAIL PROTECTED] wrote: 
 
 $str = S12345;
 $str1 = ltrim($str,S);

Good idea but you'd have problem if you have $str = SS12345; and you 
only want to get rid of the first one...

- E

__
Do You Yahoo!?
Yahoo! BB is Broadband by Yahoo!  http://bb.yahoo.co.jp/


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] splitting a string in half

2001-07-30 Thread Adam

I wish to split my databased string in half to be shown on 2 seperate
columns, but also preserve whole words. Is there a function that does this
already? Maybe a quick fix? Hopefully something that doesn't include html
tags as part of the string to split. If it's not that specific then that's
okay.

-Adam



-- 
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] Splitting a string first by quotes then spaces

2001-07-09 Thread Aaron Bennett

Ok, heres one for y'all

I have the user entering a free formed string (such as a search engine
query).. and i want to parse that string into an array of string elements...
If the user enters elements within double quotes, that would appear as one
entity.. for each word outside of containing quotes, it would be treated as
a sole entity..

For instance, the query: house shoe mother goose story
would return an array with elements:
[0] = house [1] = shoe [2] = mother goose [3] = story
(note element [2] is multiple word...)

I've been playing with explode($delimiter,$array) but if i use a double
quote there, it will want to split anything up to the first instance into a
sincle entity, then on from there...

any ideas?
--
Aaron



-- 
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] Splitting a string first by quotes then spaces

2001-07-09 Thread rm

Try this:

first test to see if the query contain quotes, if it
does, go to a seperate routine that splits the string
into an array, first however, you must make sure there
is a space before the query and one after the query (
you add these) *then* split the string into an array,
explode on the quotesyou will find that every odd
number element of the array has to be a string that
was enclosed in quotes. If you have elements 0, 1, 2
and 3, then 1 and 3 will always be strings that were
encloded in quotes. The spaces before and after the
query that you insert assure that if the user makes
the first part of the query a quote, the routine will
detect it as an odd number element.

rm

__
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail
http://personal.mail.yahoo.com/

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