| Total Complexity | 2 |
| Total Lines | 25 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php declare(strict_types=1); |
||
| 5 | final class LinkConverter |
||
| 6 | { |
||
| 7 | |||
| 8 | /** |
||
| 9 | * https://stackoverflow.com/a/10002262/1476197 |
||
| 10 | * And I added a look behind assertion to not match already converted urls. |
||
| 11 | */ |
||
| 12 | const PATTERN = '#(?<!\[|\()(?xi)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|'. |
||
| 13 | '\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))#i'; |
||
| 14 | |||
| 15 | public function convert($string): string |
||
| 16 | { |
||
| 17 | return preg_replace_callback( |
||
| 18 | self::PATTERN, |
||
| 19 | $this->callback(), |
||
| 20 | $string |
||
| 21 | ); |
||
| 22 | } |
||
| 23 | |||
| 24 | private function callback(): \Closure |
||
| 30 | }; |
||
| 31 | } |
||
| 32 | } |