0

Since I don't like using a regex I would like to ask if there's another easy way to extract values from a string.

Example strings:

send 0.2 XYZ ABC DEF

In the example above I would need to extract all values meaning I would need:

$string_1 = "send";
$string_2 = "0.2";
$string_3 = "XYZ";
$string_4 = "ABC";
$string_5 = "DEF";

Is there some way easy way to filter the above example? The " separating symbol" would be always a " " (blank space).

3

1 Answer 1

2

You can do it without any regex but using simple php function like list() and explode(). Try like this way.

<?php
$string = 'send 0.2 XYZ ABC DEF';
list($string_1, $string_2, $string_3, $string_4, $string_5) = explode(' ', $string);
echo $string_1. PHP_EOL;
echo $string_2. PHP_EOL;
echo $string_3. PHP_EOL;
echo $string_4. PHP_EOL;
echo $string_5. PHP_EOL;
?>

Program Output:

send
0.2
XYZ
ABC
DEF

SEE DEMO: https://eval.in/1022829

Edit: With for() loop,

$length = count($array);
for ($i = 0; $i < $length; $i++) {
    $name = "string_".($i+1);
    $$name = $array[$i];
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.