Iftikhar uddin
Iftikhar uddin

Reputation: 3182

How to find specific number in a string php?

I have different strings in which I want to find specific number. If the number is within the string it should print that number.

Strings are like these:

string(9) "path_12.0" 
string(9) "path_12.1" 
string(9) "path_13.0" 
string(9) "path_13.1" 
string(9) "path_13.2"

Numbers are like:

int(12) 
int(12) 
int(13) 
int(13) 
int(13)

What I tried is:

if (strpos(','.$mainString.',' , ','.$QuestionId.',') != FALSE) { 
        echo $QuestionId;
} // this doesn't print anything in the body

I also tried the below trick but it also doesn't print anything

if(in_array($QuestionId, explode(',', $mainString))) {
    echo $QuestionId;
}

I want to check something like this:

if($questionId is in $mainString) { 
      echo $questionId;
}

Note: I searched similar questions on StackOverflow but I didn't found a solution which solved my issue, therefore I'm posting this question.

Upvotes: 1

Views: 168

Answers (3)

The fourth bird
The fourth bird

Reputation: 163217

Another option could be to create an array with your strings and use preg_grep with a pattern that checks if the first part of the decimal is equal of one of the numbers.

Example of a pattern where the digits from the array are used as an alternation:

_\K(?:12|13)(?=\.\d+)
  • _\K Match underscore and forget what was matched
  • (?: Non capturing group
    • 12|13 Match either 12 or 13
  • ) Close non capturing group
  • (?=\.\d+) Positive lookahead, assert what is directly on the right is a dot and 1+ digits

For example:

$numbers = [12, 13];
$strings = [
    "path_12.0",
    "path_12.1",
    "path_13.0",
    "path_13.1",
    "path_13.2",
    "path_14.1"
];

$pattern = "/_\K(?:" . implode('|', $numbers) . ")(?=\.\d+)/";
$resullt = preg_grep($pattern, $strings);
print_r($resullt);

Result

Array
(
    [0] => path_12.0
    [1] => path_12.1
    [2] => path_13.0
    [3] => path_13.1
    [4] => path_13.2
)

Php demo

Or if you want to print the numbers only, you might use array_reduce and collect the matches:

$result = array_reduce($strings, function($carry, $item) use ($pattern){
    if (preg_match($pattern, $item, $matches)){
        $carry[] = $matches[0];

    }
    return $carry;
});

print_r($result);

Result

Array
(
    [0] => 12
    [1] => 12
    [2] => 13
    [3] => 13
    [4] => 13
)

Php demo

Upvotes: 3

Ali Abbasifard
Ali Abbasifard

Reputation: 428

$array_strings =  ["path_12.0", "path_12.1", "path_13.0", "path_13.1", "path_13.2"];
$array_numbers = [12, 22, 13, 11, 17];
$results = [];

foreach ($array_strings as $string){
  preg_match_all('!\d+\.*\d*!', $string, $matches);
  foreach ($array_numbers as $number){
      if (in_array($number, $matches[0])){
             array_push($results, $number);
         }
    }
}

print_r($results);

results: Array ( [0] => 12 [1] => 13 )

Note 1: array answers can have duplicate values.

Upvotes: 2

Rahul
Rahul

Reputation: 18557

You can use below snippet,

$paths = ["path_12.0", "path_12.1", "path_13.0", "path_13.1", "path_13.2", ];
$nos = [12, 12, 13, 13, 13, ];
function strpos_arr($needle,$haystack)
{
    if (!is_array($haystack)) {
        $haystack = [$haystack];
    }
    foreach ($haystack as $what) {
        if (($pos = strpos($what,(string)$needle)) !== false) {
            return $pos;
        }

    }
    return false;
}
foreach ($nos as $key => $value) {
    // checking if question id in in path array with str pos
    if(strpos_arr($value,$paths) !== false){
        echo $value."\n";
    }
}

Demo.

Upvotes: 2

Related Questions