@@ -26,7 +26,8 @@ class Autolink
2626 'strip_scheme ' => false ,
2727 'text_limit ' => false ,
2828 'auto_title ' => false ,
29- 'escape ' => true
29+ 'escape ' => true ,
30+ 'link_no_scheme ' => false
3031 );
3132
3233 /**
@@ -72,19 +73,32 @@ public function __construct($options = array(), $schemes = array())
7273 public function convert ($ text , $ attribs = array ())
7374 {
7475 $ self = $ this ;
76+ $ linkNoScheme = $ this ->linkNoScheme ();
7577
76- $ regex = "/(([a-zA-Z]*= \")*( " . $ this ->getSchemes (true ) . ")\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}([\/a-zA-Z0-9\-._~:?#\[\]@!$&'()*+,;=% \">]*)?)/ " ;
78+ if ($ linkNoScheme ) {
79+ $ schemeRegex = "[(%s)\:\/\/]* " ;
80+ } else {
81+ $ schemeRegex = "(%s)\:\/\/ " ;
82+ }
83+
84+ $ schemeRegex = sprintf ($ schemeRegex , $ this ->getSchemes (true ));
85+
86+ $ regex = '/(([a-zA-Z]*=")* ' . $ schemeRegex . "[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}([\/a-zA-Z0-9\-._~:?#\[\]@!$&'()*+,;=% \">]*)?)/ " ;
7787
7888 return preg_replace_callback (
7989 $ regex ,
80- function ($ matches ) use ($ self , $ attribs ) {
90+ function ($ matches ) use ($ self , $ attribs, $ linkNoScheme ) {
8191 preg_match ('/[a-zA-Z]*\=\"(.*)/ ' , $ matches [0 ], $ inElements );
8292
83- if (! $ inElements ) {
84- return $ self -> link ( $ matches [0 ], $ attribs ) ;
93+ if ($ inElements ) {
94+ return $ matches [0 ];
8595 }
8696
87- return $ matches [0 ];
97+ if ($ linkNoScheme && strpos ($ matches [0 ], ':// ' ) === 0 ) {
98+ return $ matches [0 ];
99+ }
100+
101+ return $ self ->link ($ matches [0 ], $ attribs );
88102 },
89103 $ text
90104 );
@@ -151,6 +165,12 @@ public function link($url, $attribs = array())
151165
152166 $ attribs ['href ' ] = $ this ->autoEscape () ? htmlspecialchars ($ url ) : $ url ;
153167
168+ if (($ scheme = $ this ->linkNoScheme ()) && strpos ($ attribs ['href ' ], ':// ' ) === false ) {
169+ $ scheme = is_string ($ scheme ) ? $ scheme : 'http ' ;
170+
171+ $ attribs ['href ' ] = $ scheme . ':// ' . $ attribs ['href ' ];
172+ }
173+
154174 if ($ this ->autoTitle ()) {
155175 $ attribs ['title ' ] = htmlspecialchars ($ url );
156176 }
@@ -246,6 +266,18 @@ public function autoTitle($value = null)
246266 return $ this ->optionAccess ('auto_title ' , $ value );
247267 }
248268
269+ /**
270+ * linkNoScheme
271+ *
272+ * @param mixed $value
273+ *
274+ * @return mixed|static
275+ */
276+ public function linkNoScheme ($ value = null )
277+ {
278+ return $ this ->optionAccess ('link_no_scheme ' , $ value );
279+ }
280+
249281 /**
250282 * optionAccess
251283 *
0 commit comments