1

I do have the following pregmatch:

$search = 310850;

if (preg_match("/\b$search\b/i", $response)) {
        echo "match !";
    } else {
        echo "NO MATCH";
    }

response contains alot of things, but it contains 310850 and also 3108502 and 31085.

to info: $response contains a html page with alot of elements that contains the $search

How can i make sure i get a match on 310850and not all the others?

EDIT:

How do i allow the characters < and > to be next to the search? they could be wrapped on ded123>number<blabla

maria
  • 207
  • 5
  • 22
  • 56

1 Answers1

0
<?php

$search = 310850;

$responses = ["310850"," 310850 ", "0310850","3108501",

"<html>
<a href='google.com'>310850</a>
</html>
"];

function test($search, $response){
    echo $response,": ";
    if (preg_match("/([^0-9]+|^)$search([^0-9]+|$)/i", $response)) {
        echo "match !";
    } else {
        echo "NO MATCH";
    }
    echo "\n";
}

foreach($responses as $response){
    test($search, $response);
}

output:

php -q a.php
310850: match !
 310850 : match !
0310850: NO MATCH
3108501: NO MATCH
<html>
<a href='google.com'>310850</a>
</html>
: match !
jancha
  • 4,916
  • 1
  • 24
  • 39