| | |
splitting a string with preg_split using multiple delimiters
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Jul 2007
Posts: 34
Reputation:
Solved Threads: 0
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:
However, this echos out the entire thing eg: 128px
If I try something like this,
It works perfectly if I search for 128px. However, it fails for everything else like 128em or 128%.
Any ideas?
Thanks!
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)
$test = preg_split('(px)(em)(pt)(%))','128px','-1', PREG_SPLIT_DELIM_CAPTURE); echo $test;
However, this echos out the entire thing eg: 128px
If I try something like this,
PHP Syntax (Toggle Plain Text)
$test = preg_split('(px)|(em)|(pt)|(%))','128px','-1', PREG_SPLIT_DELIM_CAPTURE); echo $test;
It works perfectly if I search for 128px. However, it fails for everything else like 128em or 128%.
Any ideas?
Thanks!
0
#4 Oct 7th, 2009
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:
I made it a backreference, but it is probably not necessary.
The code should be:
php Syntax (Toggle Plain Text)
$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
-- NASA
•
•
Join Date: Sep 2009
Posts: 525
Reputation:
Solved Threads: 61
1
#5 Oct 7th, 2009
PHP Syntax (Toggle Plain Text)
$str = "20px 100% dfsd 10em "; $test = preg_split('(px|em|pt|%)',$str,-1, PREG_SPLIT_OFFSET_CAPTURE); //echo '<pre>'; print_r($test); //echo '</pre>';
0
#6 Oct 7th, 2009
php Syntax (Toggle Plain Text)
$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)
$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
-- NASA
•
•
Join Date: Sep 2009
Posts: 525
Reputation:
Solved Threads: 61
0
#7 Oct 7th, 2009
This seperates them all but it isn't the desired yet,
the guy wants it something like
and we getting Array ( [0] => [1] => 20 [2] => [3] => px [4] => [5] => 100 [6] => [7] => % [8] => dfsd [9] => 10 [10] => [11] => em [12] => )
with
the guy wants it something like
PHP Syntax (Toggle Plain Text)
Array ( [0] => [1] => 20 [2] =>px ) Array([0] => 100 [1] => % )Array([0] => 10 [1] => em )
with
PHP Syntax (Toggle Plain Text)
$test = preg_split('/(\d+|px|em|pt|%)/',$str,-1, PREG_SPLIT_DELIM_CAPTURE);
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.
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
-- NASA
•
•
Join Date: Jul 2007
Posts: 34
Reputation:
Solved Threads: 0
0
#9 Oct 7th, 2009
•
•
•
•
php Syntax (Toggle Plain Text)
$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)
$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)
$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!
![]() |
Similar Threads
- Splitting a string (Java)
- Preg_Match? Explode? Please Help :D! (PHP)
- Splitting String (Python)
- Getline with multiple delimiters?? (C++)
- Splitting a string? (C)
Other Threads in the PHP Forum
- Previous Thread: posting from onClick generated fields
- Next Thread: Sandbox Paypal subscription button ?
| Thread Tools | Search this Thread |
apache api array beginner binary broken cache cakephp checkbox class cms code confirm cron curl customizableitems database date display dynamic echo email error external fcc file files folder form forms forum freelancing function functions google header headmethod howtowriteathesis href htaccess html iframe image include insert ip javascript joomla limit link login mail malfunction menu method mlm mod_rewrite multiple mysql neutrality oop pageing pagerank paypal pdf php phpmysql play problem query question radio random recursion remote root script search select server sessions sms soap source space sql support! syntax system table template tutorial update upload url validator variable video web youtube





