arrays - PHP: Explode string on more than 2 dashes in string

206

I want explode a string to array with 2 dashes or more. For example I have a string such as "size--medium" or "size---medium" or "size----medium" or this pattern with more than 2 dashes, I want explode them to:

Array(
    0 => size,
    1 => medium
);

Thanks guys

438

Answer

Solution:

Just split the input string according to two or more dashes.

<?php
$yourstring = "size----medium";
$regex = '~-{2,}~';
$splits = preg_split($regex, $yourstring);
print_r($splits);
?>

Output:

Array
(
    [0] => size
    [1] => medium
)

People are also looking for solutions to the problem: mysql - Speed up PHP recursive function

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.