Skip to content

Commit 2f13e05

Browse files
committed
Link without scheme #3
1 parent 66585b3 commit 2f13e05

File tree

3 files changed

+82
-7
lines changed

3 files changed

+82
-7
lines changed

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ $options = array(
4242
'strip_scheme' => false,
4343
'text_limit' => false,
4444
'auto_title' => false,
45-
'escape' => true
45+
'escape' => true,
46+
'link_no_scheme' => false
4647
);
4748

4849
$schemes = array('http', 'https', 'skype', 'itunes');
@@ -205,6 +206,23 @@ Output
205206
<a href="http://www.google.com.tw?foo=bar&yoo=baz" >http://www.google.com.tw?foo=bar&yoo=baz</a>
206207
```
207208

209+
### `link_no_scheme`
210+
211+
Convert URL which no scheme. If you pass `TRUE` to this option, Autolink will use
212+
`http` as default scheme, you can also provide your own default scheme.
213+
214+
``` php
215+
$auitolink->linkNoScheme('https');
216+
217+
$text = $autolink->convert('www.google.com.tw');
218+
```
219+
220+
Output
221+
222+
``` html
223+
<a href="https://www.google.com.tw" >www.google.com.tw</a>
224+
```
225+
208226
## Scheme
209227

210228
You can add new scheme to convert URL begin with it, for example: `vnc://example.com`

src/Autolink.php

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*

test/AutolinkTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,31 @@ public function testAddScheme()
214214
$this->assertEquals('<a href="' . $url . '">' . $url . '</a>', $this->instance->convert($url));
215215
}
216216

217+
public function testLinkNoScheme()
218+
{
219+
$this->instance->linkNoScheme('http');
220+
221+
$url = 'ftp://example.com';
222+
223+
$this->assertEquals('<a href="' . $url . '">' . $url . '</a>', $this->instance->convert($url));
224+
225+
$url = 'example.com';
226+
227+
$this->assertEquals('<a href="http://' . $url . '">' . $url . '</a>', $this->instance->convert($url));
228+
229+
$url = 'https://example.com';
230+
231+
$this->assertEquals('<a href="' . $url . '">' . $url . '</a>', $this->instance->convert($url));
232+
233+
$url = 'skype://example.com';
234+
235+
$this->assertEquals($url, $this->instance->convert($url));
236+
237+
$this->instance->addScheme('skype');
238+
239+
$this->assertEquals('<a href="' . $url . '">' . $url . '</a>', $this->instance->convert($url));
240+
}
241+
217242
/**
218243
* testGetAndSetScheme
219244
*

0 commit comments

Comments
 (0)