A
A
Anton Vertov2018-03-20 01:22:27
PHP
Anton Vertov, 2018-03-20 01:22:27

How to find text in a variable (string)?

Good day, friends. I have a variable (string) like this "10 01 32 05 54".
I need to find in this string all the numbers from 01 to 09 , that is, 01 and 05 are suitable for me .
Can you please tell me how to search in a variable by several conditions at the same time? (find 01 and 02 and 03 etc. )

Answer the question

In order to leave comments, you need to log in

3 answer(s)
X
xmoonlight, 2018-03-20
@1Frosty

$re = '/(?:[^\d]|^)([0][1-9])(?:[^\d]|$)/';
$str = '10 01 32 05 54';

preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);

// Print the entire match result
var_dump($matches);

S
Stalker_RED, 2018-03-20
@Stalker_RED

Regular: /0\d/
https://regex101.com/r/msssCF/2

I
Immortal_pony, 2018-03-20
@Immortal_pony

$string = "10 01 32 05 54";
$numbers = explode(" ", $string);
$filtered = array_filter($numbers , function($number) { 
    return in_array($number, ["01", "02", "03", "04", "05"]);
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question