-1

I wanted to check if a string contains specific words? According How do I check if a string contains a specific word? I can use strpos() function.

Sample Code I wrote

if(strpos($value, 'filters(10)') === false){
    //Do something
} else {
    //Do Another Thing
}

If I need to check specific word filters(10) above functions works fine. But I need to check all numbers above 10 with filter word.

Example

$value = 'bla bla  bla bla' // Should return false
$value = 'bla bla filters(1) bla bla' // Should return false
$value = 'bla bla filters(5) bla bla' // Should return false
$value = 'bla bla filters(10) bla bla' // Should return true
$value = 'bla bla filters(15) bla bla' // Should return true
$value = 'bla bla filters(20) bla bla' // Should return true

How to modify my code to get above results?

LF00
  • 27,015
  • 29
  • 156
  • 295

2 Answers2

1

Here you can use Regex for getting that particular no. and then apply further conditions.

Regex: filters\((\d+)\)

This will match filters( and then capture digits and then )

Try this code snippet here

preg_match("#filters\((\d+)\)#", $value, $matches);
if(isset($matches[1]) && $matches[1]>10)
{
    //do something on true
}
else
{
    //do something on false
}
Sahil Gulati
  • 15,028
  • 4
  • 24
  • 42
  • 1
    Thanks. This worked perfectly fine. Even I read lots of article about preg_match still I can't impliment something like what you have written my self... Can you provide a link to which explain how to write preg_match easily... Thanks. – I am the Most Stupid Person Sep 29 '17 at 04:18
0

You can parse the element in filters() and compare it with 10.

if(($pos = strpos($value, 'filters(10)')) === false){
    if(intval(substr($value, $pos + strlen('filters('))) > 10) {
    //Do something
    } else {// do another thing}
} else {
    //Do Another Thing
}
LF00
  • 27,015
  • 29
  • 156
  • 295