Home > Back-end >  Php search for a pattern in a string
Php search for a pattern in a string

Time:01-05

Can someone help me to find the job id from the text below:

$search_string = '/jobs/17 has no compatible crew with sufficient capacity (job: 304390)';

I have tried the following but did not have any luck:

preg_match("/\[job: ([A-Za-z\/] )\]/", $search_string, $match);
print_r($match) --showing empty

CodePudding user response:

You should match parenthesis instead, and capture 1 or more digits. Then you can get the group 1 value:

\(job: (\d )\)/

Example

$search_string = '/jobs/17 has no compatible crew with sufficient capacity (job: 304390)';
if (preg_match("/\(job: (\d )\)/", $search_string, $match)) {
    print_r($match[1]);
}

Output

304390
  • Related