Parsing string using regular expressions

suggest change

preg_match can be used to parse a string with a regular expression. The parts of the expression enclosed in parentheses are called subpatterns, and with them you can pick individual parts of the string.

$str = "<a href=\"http://example.org\">My Link</a>";
$pattern = "/<a href=\"(.*)\">(.*)<\/a>/";
$result = preg_match($pattern, $str, $matches);
if($result === 1) {
    // The string matches the expression
    print_r($matches);
} else if($result === 0) {
    // No match
} else {
    // Error occurred
}
Array
(
    [0] => <a href="http://example.org">My Link</a>
    [1] => http://example.org
    [2] => My Link
)

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents