943,997 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 2266
  • PHP RSS
Oct 6th, 2009
0

splitting a string with preg_split using multiple delimiters

Expand Post »
Hi,

I'm trying to split a string with multiple delimiters using the PHP preg_split function.

I am trying to separate the numbers from the units in terms like 172px, 100%, 2.5ems etc...

i.e. after I run a preg_split on 172px I want the resulting array to have array[0] = 172 and array[1] = px

Here is the code I've used so far:

PHP Syntax (Toggle Plain Text)
  1. $test = preg_split('(px)(em)(pt)(%))','128px','-1', PREG_SPLIT_DELIM_CAPTURE);
  2.  
  3. echo $test;


However, this echos out the entire thing eg: 128px

If I try something like this,

PHP Syntax (Toggle Plain Text)
  1. $test = preg_split('(px)|(em)|(pt)|(%))','128px','-1', PREG_SPLIT_DELIM_CAPTURE);
  2.  
  3. echo $test;

It works perfectly if I search for 128px. However, it fails for everything else like 128em or 128%.


Any ideas?

Thanks!
Similar Threads
Reputation Points: 10
Solved Threads: 1
Light Poster
baltazar is offline Offline
46 posts
since Jul 2007
Oct 7th, 2009
1
Re: splitting a string with preg_split using multiple delimiters
(px|em|pt|%)
Sponsor
Featured Poster
Reputation Points: 550
Solved Threads: 731
Bite my shiny metal ass!
pritaeas is offline Offline
4,182 posts
since Jul 2006
Oct 7th, 2009
0
Re: splitting a string with preg_split using multiple delimiters
Click to Expand / Collapse  Quote originally posted by pritaeas ...
(px|em|pt|%)
Can you elaborate the syntax a bit more..
Reputation Points: 29
Solved Threads: 76
Practically a Master Poster
network18 is offline Offline
616 posts
since Sep 2009
Oct 7th, 2009
0
Re: splitting a string with preg_split using multiple delimiters
Click to Expand / Collapse  Quote originally posted by pritaeas ...
(px|em|pt|%)
He wants to split his string on 'px' or 'em' or 'pt' or '%'
I made it a backreference, but it is probably not necessary.

The code should be:
php Syntax (Toggle Plain Text)
  1. $test = preg_split('/px|em|pt|%/', '128px', -1, PREG_SPLIT_DELIM_CAPTURE);
Last edited by pritaeas; Oct 7th, 2009 at 6:08 am.
Sponsor
Featured Poster
Reputation Points: 550
Solved Threads: 731
Bite my shiny metal ass!
pritaeas is offline Offline
4,182 posts
since Jul 2006
Oct 7th, 2009
1
Re: splitting a string with preg_split using multiple delimiters
PHP Syntax (Toggle Plain Text)
  1. $str = "20px 100% dfsd 10em ";
  2. $test = preg_split('(px|em|pt|%)',$str,-1, PREG_SPLIT_OFFSET_CAPTURE);
  3. //echo '<pre>';
  4. print_r($test);
  5. //echo '</pre>';
This isn't working either...what can be the code for it
Reputation Points: 29
Solved Threads: 76
Practically a Master Poster
network18 is offline Offline
616 posts
since Sep 2009
Oct 7th, 2009
0
Re: splitting a string with preg_split using multiple delimiters
php Syntax (Toggle Plain Text)
  1. $test = preg_split('/px|em|pt|%/',$str,-1, PREG_SPLIT_OFFSET_CAPTURE);

You should put your regex between slashes. Is this what you want ? Perhaps you want to split also before a number ?

php Syntax (Toggle Plain Text)
  1. $test = preg_split('/(\d+|px|em|pt|%)/',$str,-1, PREG_SPLIT_DELIM_CAPTURE);

I know see that you need to use the parenthesis with the PREG_SPLIT_DELIM_CAPTURE option.
Last edited by pritaeas; Oct 7th, 2009 at 6:29 am.
Sponsor
Featured Poster
Reputation Points: 550
Solved Threads: 731
Bite my shiny metal ass!
pritaeas is offline Offline
4,182 posts
since Jul 2006
Oct 7th, 2009
0
Re: splitting a string with preg_split using multiple delimiters
This seperates them all but it isn't the desired yet,
the guy wants it something like
PHP Syntax (Toggle Plain Text)
  1. Array ( [0] => [1] => 20 [2] =>px ) Array([0] => 100 [1] => % )Array([0] => 10 [1] => em )
and we getting Array ( [0] => [1] => 20 [2] => [3] => px [4] => [5] => 100 [6] => [7] => % [8] => dfsd [9] => 10 [10] => [11] => em [12] => )
with
PHP Syntax (Toggle Plain Text)
  1. $test = preg_split('/(\d+|px|em|pt|%)/',$str,-1, PREG_SPLIT_DELIM_CAPTURE);
Reputation Points: 29
Solved Threads: 76
Practically a Master Poster
network18 is offline Offline
616 posts
since Sep 2009
Oct 7th, 2009
0
Re: splitting a string with preg_split using multiple delimiters
His example only contains '128px', post #4 splits that fine.

Once you introduce other chars into the equasion, I think it's best to remove them first. I'll wait for feedback first from baltazar, and if there is anything else he wants, he should post some more test strings with expected results.
Sponsor
Featured Poster
Reputation Points: 550
Solved Threads: 731
Bite my shiny metal ass!
pritaeas is offline Offline
4,182 posts
since Jul 2006
Oct 7th, 2009
0
Re: splitting a string with preg_split using multiple delimiters
Click to Expand / Collapse  Quote originally posted by pritaeas ...
php Syntax (Toggle Plain Text)
  1. $test = preg_split('/px|em|pt|%/',$str,-1, PREG_SPLIT_OFFSET_CAPTURE);

You should put your regex between slashes. Is this what you want ? Perhaps you want to split also before a number ?

php Syntax (Toggle Plain Text)
  1. $test = preg_split('/(\d+|px|em|pt|%)/',$str,-1, PREG_SPLIT_DELIM_CAPTURE);

I know see that you need to use the parenthesis with the PREG_SPLIT_DELIM_CAPTURE option.

Hi guys... thank you so much for giving your time to my problem.

Pritaeas, your solution worked.

For the record, I have a single variable that contains the width of a div. for example: 128px or 100% or 50em. There will be only one of the above, not all of them. I want to split the number from the unit.

So using:

php Syntax (Toggle Plain Text)
  1. $test = preg_split('/(|px|em|pt|%)/',$str,-1, PREG_SPLIT_DELIM_CAPTURE);

works perfectly.

Thank you all once again and hopefully I shall be able to return the favour someday. Cheers!
Reputation Points: 10
Solved Threads: 1
Light Poster
baltazar is offline Offline
46 posts
since Jul 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: posting from onClick generated fields
Next Thread in PHP Forum Timeline: Sandbox Paypal subscription button ?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC