Last active
February 10, 2020 16:59
-
-
Save michaelperrin/5153567a12c74d7ceeb662b37056579f to your computer and use it in GitHub Desktop.
Advanced regex features for Markdown links, example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$text = 'Text with two links [inline1](https://www.nrk.no) and [inline2](https://www.yr.no), may occur.'; | |
$regex = <<<'REGEX' | |
(?<text_group> # Text group, including square brackets | |
\[ | |
(?> # (?> defines an atomic group, this is a performance improvement when using recursion | |
[^\[\]]+ # Look for any char except closing square bracket | |
|(?&text_group) # OR: find recursively an other pattern with opening and closing square brackets | |
)* | |
\] | |
) | |
(?: | |
\( | |
(?<url>\S*?) # URL: non-greedy non-whitespace characters | |
(?: | |
[ ] | |
" | |
(?<title> | |
(?:[^"]|(?<=\\)")*? # Title without double quotes around | |
) | |
" | |
)? | |
\) | |
) | |
REGEX; | |
preg_match_all("`$regex`x", $text, $matches, PREG_SET_ORDER); | |
var_dump(json_encode($matches, JSON_PRETTY_PRINT)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment