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:

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

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

(px|em|pt|%)

(px|em|pt|%)

Can you elaborate the syntax a bit more..

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

$test = preg_split('/px|em|pt|%/', '128px', -1, PREG_SPLIT_DELIM_CAPTURE);
$str = "20px 100% dfsd 10em ";
$test = preg_split('(px|em|pt|%)',$str,-1, PREG_SPLIT_OFFSET_CAPTURE);
//echo '<pre>';
print_r($test);
//echo '</pre>';

This isn't working either...what can be the code for it

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

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

This seperates them all but it isn't the desired yet,
the guy wants it something like

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

$test = preg_split('/(\d+|px|em|pt|%)/',$str,-1, PREG_SPLIT_DELIM_CAPTURE);

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.

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

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.