Search and returns the first results on a regex search


/ Published in: PHP
Save to your folder(s)

Search and returns the first results on a regex search
If we use parenthesized or subpattern, can return with $result_number


Copy this code and paste it in your HTML
  1. function preg_match_first($pattern,$string,$result_number=null)
  2. {
  3. if(is_null($result_number)) $result_number=0;
  4. preg_match($pattern,$string,$matches);
  5. return !empty($matches)?$matches[$result_number]:"";
  6. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.