| 108 | | $text_matches[$key] = str_replace( |
|---|
| 109 | | "\\'", |
|---|
| 110 | | "'", |
|---|
| 111 | | preg_replace( |
|---|
| 112 | | array( |
|---|
| 113 | | '#\b([a-z]{3,}://[a-z0-9%\$\-_.+!*;/?:@=&\'\#,]+[a-z0-9\$\-_+!*;/?:@=&\'\#,])\b#ie', # Fully URLs |
|---|
| 114 | | '#\b(www\.([a-z0-9\-]+\.)+[a-z]{2,}(?:/[a-z0-9%\$\-_.+!*;/?:@=&\'\#,]+[a-z0-9\$\-_+!*;/?:@=&\'\#,])?)\b#ie', # www. domains |
|---|
| 115 | | '#\b([a-z0-9\\.+\'_\\-]+@(?:[a-z0-9\\-]+\.)+[a-z]{2,})\b#ie' # email addresses |
|---|
| 116 | | ), |
|---|
| 117 | | array( |
|---|
| 118 | | '"<a href=\"\1\">" . ' . $replacement . ' . "</a>"', |
|---|
| 119 | | '"<a href=\"http://\1\">" . ' . $replacement . ' . "</a>"', |
|---|
| 120 | | '"<a href=\"mailto:\1\">" . ' . $replacement . ' . "</a>"' |
|---|
| 121 | | ), |
|---|
| 122 | | $text |
|---|
| 123 | | ) |
|---|
| | 101 | preg_match_all( |
|---|
| | 102 | '~ |
|---|
| | 103 | \b([a-z]{3,}://[a-z0-9%\$\-_.+!*;/?:@=&\'\#,]+[a-z0-9\$\-_+!*;/?:@=&\'\#,])\b | # Fully URLs |
|---|
| | 104 | \b(www\.(?:[a-z0-9\-]+\.)+[a-z]{2,}(?:/[a-z0-9%\$\-_.+!*;/?:@=&\'\#,]+[a-z0-9\$\-_+!*;/?:@=&\'\#,])?)\b | # www. domains |
|---|
| | 105 | \b([a-z0-9\\.+\'_\\-]+@(?:[a-z0-9\\-]+\.)+[a-z]{2,})\b # email addresses |
|---|
| | 106 | ~ix', |
|---|
| | 107 | $text, |
|---|
| | 108 | $matches, |
|---|
| | 109 | PREG_SET_ORDER |
|---|
| | 111 | |
|---|
| | 112 | // For each match we find the first occurence, replace it and then |
|---|
| | 113 | // start from the end of that finding the next occurence. This |
|---|
| | 114 | // prevents double linking of matches for http://www.example.com and |
|---|
| | 115 | // www.example.com |
|---|
| | 116 | $last_pos = 0; |
|---|
| | 117 | foreach ($matches as $match) { |
|---|
| | 118 | $match_pos = strpos($text, $match[0], $last_pos); |
|---|
| | 119 | $length = strlen($match[0]); |
|---|
| | 120 | $prefix = ''; |
|---|
| | 121 | |
|---|
| | 122 | if (!empty($match[3])) { |
|---|
| | 123 | $prefix = 'mailto:'; |
|---|
| | 124 | } elseif (!empty($match[2])) { |
|---|
| | 125 | $prefix = 'http://'; |
|---|
| | 126 | } |
|---|
| | 127 | |
|---|
| | 128 | $replacement = '<a href="' . $prefix . $match[0] . '">'; |
|---|
| | 129 | $replacement .= ($link_text_length && strlen($match[0]) > $link_text_length) ? substr($match[0], 0, $link_text_length) . "…" : $match[0]; |
|---|
| | 130 | $replacement .= '</a>'; |
|---|
| | 131 | |
|---|
| | 132 | $text = substr_replace( |
|---|
| | 133 | $text, |
|---|
| | 134 | $replacement, |
|---|
| | 135 | $match_pos, |
|---|
| | 136 | $length |
|---|
| | 137 | ); |
|---|
| | 138 | |
|---|
| | 139 | $last_pos = $match_pos + strlen($replacement); |
|---|
| | 140 | } |
|---|
| | 141 | |
|---|
| | 142 | $text_matches[$key] = $text; |
|---|