Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Php search for a pattern in a string

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading