forked from mcrumm/phlack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkFormatter.php
More file actions
36 lines (32 loc) · 1.15 KB
/
LinkFormatter.php
File metadata and controls
36 lines (32 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php
namespace Crummy\Phlack\Common\Formatter;
class LinkFormatter implements FormatterInterface
{
const HTML = '/ <a (?:.*?) href=[\'"](.+)[\'"] (?:.*)> (.+?) <\/a> /x';
const MARKDOWN = '/\[(.*?)\]\((.+?)\)/';
/**
* @param string $text
* @return string
*/
public function format($text)
{
$text = preg_replace_callback(self::HTML, $this->getMatchFormatter(), $text);
$text = preg_replace_callback(self::MARKDOWN, $this->getMatchFormatter(2, 1), $text);
return $text;
}
/**
* @param integer $linkIdx The index of the matches containing the resource link
* @param integer $labelIdx The index of the matches containing the label (if one exists)
* @return \Closure A callable to be passed to preg_replace_callback
*/
protected function getMatchFormatter($linkIdx = 1, $labelIdx = 2)
{
return function (array $matches) use ($linkIdx, $labelIdx) {
$out = '<';
$out .= $matches[$linkIdx];
if (!empty($matches[$labelIdx])) { $out .= '|' . $matches[$labelIdx]; }
$out .= '>';
return $out;
};
}
}