php - How to split string using one pattern?
Get the solution ↓↓↓Solution:
You can combine all regex's into one to extract all values into$match
at once:
$string = "84 - Pampers mid (4-9кг) №180 [Procter&Gamble] - 1978.00";
preg_match('/(\d+) - (.*) \[(.*)\] - (\d+.\d+)/', $string, $match);
After running this code$match
contains:
Array
(
[0] => 84 - Pampers mid (4-9кг) №180 [Procter&Gamble] - 1978.00
[1] => 84
[2] => Pampers mid (4-9кг) №180
[3] => Procter&Gamble
[4] => 1978.00
)
If you have a lot of these strings in an array you might consider using preg_match_all
Answer
Solution:
something like
$x = '84 - Pampers mid (4-9кг) №180 [Procter&Gamble] - 1978.00';
preg_match_all('/(\d*)\ \-\ (.*)\ \[(.*)\]\ \-\ (.*)/', $x, $matches);
var_dump($matches);
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: the process class relies on proc_open, which is not available on your php installation.
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.
Similar questions
Find the answer in similar questions on our website.
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.