splitting a string with preg_split using multiple delimiters

Thread Solved

Join Date: Jul 2007
Posts: 34
Reputation: baltazar is an unknown quantity at this point 
Solved Threads: 0
baltazar baltazar is offline Offline
Light Poster

splitting a string with preg_split using multiple delimiters

 
0
  #1
Oct 6th, 2009
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:

  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,

  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!
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 831
Reputation: pritaeas will become famous soon enough pritaeas will become famous soon enough 
Solved Threads: 136
Sponsor
pritaeas's Avatar
pritaeas pritaeas is offline Offline
Practically a Posting Shark
 
1
  #2
Oct 7th, 2009
(px|em|pt|%)
"If it is NOT source, it is NOT software."
-- NASA
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 525
Reputation: network18 is an unknown quantity at this point 
Solved Threads: 61
network18 network18 is offline Offline
Posting Pro
 
0
  #3
Oct 7th, 2009
Originally Posted by pritaeas View Post
(px|em|pt|%)
Can you elaborate the syntax a bit more..
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 831
Reputation: pritaeas will become famous soon enough pritaeas will become famous soon enough 
Solved Threads: 136
Sponsor
pritaeas's Avatar
pritaeas pritaeas is offline Offline
Practically a Posting Shark
 
0
  #4
Oct 7th, 2009
Originally Posted by pritaeas View Post
(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:
  1. $test = preg_split('/px|em|pt|%/', '128px', -1, PREG_SPLIT_DELIM_CAPTURE);
Last edited by pritaeas; Oct 7th, 2009 at 6:08 am.
"If it is NOT source, it is NOT software."
-- NASA
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 525
Reputation: network18 is an unknown quantity at this point 
Solved Threads: 61
network18 network18 is offline Offline
Posting Pro
 
1
  #5
Oct 7th, 2009
  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
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 831
Reputation: pritaeas will become famous soon enough pritaeas will become famous soon enough 
Solved Threads: 136
Sponsor
pritaeas's Avatar
pritaeas pritaeas is offline Offline
Practically a Posting Shark
 
0
  #6
Oct 7th, 2009
  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 ?

  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.
"If it is NOT source, it is NOT software."
-- NASA
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 525
Reputation: network18 is an unknown quantity at this point 
Solved Threads: 61
network18 network18 is offline Offline
Posting Pro
 
0
  #7
Oct 7th, 2009
This seperates them all but it isn't the desired yet,
the guy wants it something like
  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
  1. $test = preg_split('/(\d+|px|em|pt|%)/',$str,-1, PREG_SPLIT_DELIM_CAPTURE);
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 831
Reputation: pritaeas will become famous soon enough pritaeas will become famous soon enough 
Solved Threads: 136
Sponsor
pritaeas's Avatar
pritaeas pritaeas is offline Offline
Practically a Posting Shark
 
0
  #8
Oct 7th, 2009
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.
"If it is NOT source, it is NOT software."
-- NASA
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 34
Reputation: baltazar is an unknown quantity at this point 
Solved Threads: 0
baltazar baltazar is offline Offline
Light Poster
 
0
  #9
Oct 7th, 2009
Originally Posted by pritaeas View Post
  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 ?

  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:

  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!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC