Extracting matches from php regex
Asked Answered
C

6

28

In perl regex we can extract the matched variables, ex below.

   # extract hours, minutes, seconds
   $time =~ /(\d\d):(\d\d):(\d\d)/; # match hh:mm:ss format
   $hours = $1;
   $minutes = $2;
   $seconds = $3;

How to do this in php?

$subject = "E:[email protected] I:100955";
$pattern = "/^E:/";
if (preg_match($pattern, $subject)) {
    echo "Yes, A Match";
}

How to extract the email from there? (We can explode it and get it...but would like a method to get it directly through regex)?

Credulity answered 29/9, 2009 at 14:40 Comment(0)
P
42

Try using the named subpattern syntax of preg_match:

<?php

$str = 'foobar: 2008';

// Works in PHP 5.2.2 and later.
preg_match('/(?<name>\w+): (?<digit>\d+)/', $str, $matches);

// Before PHP 5.2.2, use this:
// preg_match('/(?P<name>\w+): (?P<digit>\d+)/', $str, $matches);

print_r($matches);

?>

Output:

 Array (
     [0] => foobar: 2008
     [name] => foobar
     [1] => foobar
     [digit] => 2008
     [2] => 2008 )
Potamic answered 29/9, 2009 at 14:49 Comment(0)
W
11

Check the php manual

int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags [, int $offset ]]] )

If matches is provided, then it is filled with the results of search. $matches[0] will contain the text that matched the full pattern, $matches1 will have the text that matched the first captured parenthesized subpattern, and so on.

$subject = "E:[email protected] I:100955";
$pattern = "/^E:(?<contact>\w+) I:(?<id>\d+)$/";
if (preg_match($pattern, $subject,$matches)) {
    print_r($matches);
}
Wisconsin answered 29/9, 2009 at 14:46 Comment(0)
M
4

The simpler solution

  1. go to regex101.com
  2. Use the great documentation for creating and testing your regex (make sure you select PHP).
  3. In the TOOLS section click on code generation

  4. This is an example of what I got.

$re = '/\#.*@hello\((?<operator>\w+),?(?<args>.*)\).*/m';
$str = ' Testing string
# @hello(group_fields)
# @hello(operator, arguments, more arguments)';

preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);

// Print the entire match result
var_dump($matches);
Molder answered 8/5, 2019 at 21:55 Comment(0)
K
3

Use the matchs parameter of the preg_match function as follows:

matches:
If matches is provided, then it is filled with the results of search. $matches[0] will contain the text that matched the full pattern, $matches[1] will have the text that matched the first captured parenthesized subpattern, and so on.

Kuth answered 29/9, 2009 at 14:46 Comment(0)
E
3

You can just modify your current regexp to capture everything after the colon up to the first space:

$subject = "E:[email protected] I:100955";
$pattern = "/^E:([^ ]+)/";
if (preg_match($pattern, $subject, $m)) {
    echo "Yes, A Match";
}
$email = $m[1];

In case you're not familiar with regexp, [^ ]+ means "any character but a space" and it doesn't require a space to be present to work. If for any reason the input changes to "E:[email protected]" without the " I:12345" bit, it will still work.

Empire answered 29/9, 2009 at 14:53 Comment(0)
T
0

There is no direct function to find! But you can have your own functions like these:

function preg_find($pattern, $text, $def = null){
    $matches = PREG_FindAll($pattern, $text);
    return isset($matches[0])?$matches[0]:$def;
}

function preg_findall($pattern, $text){
    preg_match($pattern, $text, $matches);
    return $matches;
}

Enjoy...

Treasonous answered 2/1, 2023 at 9:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.