@@ -13,14 +13,19 @@ A library to auto convert URLs to links.
13
13
* [ Scheme] ( #scheme )
14
14
* [ Link Builder] ( #link-builder )
15
15
16
+ ## Requirement
17
+
18
+ - Version 2.x require PHP 8.0 or higher.
19
+ - Version 1.x supports PHP 5.3 to 7.4
20
+
16
21
## Installation via Composer
17
22
18
23
Add this to composer.json require block.
19
24
20
25
``` json
21
26
{
22
27
"require" : {
23
- "asika/autolink" : " 1.* "
28
+ "asika/autolink" : " ^2.0 "
24
29
}
25
30
}
26
31
```
@@ -29,35 +34,35 @@ Add this to composer.json require block.
29
34
30
35
This is a quick start to convert URL to link:
31
36
32
- ``` php
33
- use Asika\Autolink\Linker ;
37
+ ``` php
38
+ use Asika\Autolink\AutolinkStatic ;
34
39
35
- $text = Linker ::convert($text);
36
- $text = Linker ::convertEmail($text);
40
+ $text = AutolinkStatic ::convert($text);
41
+ $text = AutolinkStatic ::convertEmail($text);
37
42
```
38
43
39
44
## Use Autolink Object
40
45
41
46
Create the object:
42
47
43
- ``` php
48
+ ``` php
44
49
use Asika\Autolink\Autolink;
45
50
46
- $autolink = new Autolink;
51
+ $autolink = new Autolink() ;
47
52
```
48
53
49
54
Create with options.
50
55
51
- ``` php
52
- $options = array(
56
+ ``` php
57
+ $options = [
53
58
'strip_scheme' => false,
54
59
'text_limit' => false,
55
60
'auto_title' => false,
56
61
'escape' => true,
57
62
'link_no_scheme' => false
58
- ) ;
63
+ ] ;
59
64
60
- $schemes = array( 'http', 'https', 'skype', 'itunes') ;
65
+ $schemes = [ 'http', 'https', 'skype', 'itunes'] ;
61
66
62
67
$autolink = new Autolink($options, $schemes);
63
68
```
@@ -79,7 +84,7 @@ http://example.com/?foo[1]=a&foo[2]=b
79
84
80
85
We convert all URLs.
81
86
82
- ``` php
87
+ ``` php
83
88
$text = $autolink->convert($text);
84
89
```
85
90
@@ -98,13 +103,13 @@ This is URL with multi-level query:
98
103
99
104
### Add Attributes
100
105
101
- ``` php
102
- $text = $autolink->convert($text, array( 'class' => 'center') );
106
+ ``` php
107
+ $text = $autolink->convert($text, [ 'class' => 'center'] );
103
108
```
104
109
105
110
All link will add this attributes:
106
111
107
- ``` php
112
+ ``` php
108
113
This is Simple URL:
109
114
<a href =" http://www.google.com.tw" class =" center" >http://www.google.com.tw</a >
110
115
@@ -116,7 +121,7 @@ This is SSL URL:
116
121
117
122
Email url has no scheme, we use anoter method to convert them, and it will add ` mailto: ` at begin of ` href ` .
118
123
119
- ``` php
124
+ ``` php
120
125
$text = $aurolink->convertEmail($text);
121
126
```
122
127
@@ -133,7 +138,7 @@ Output
133
138
134
139
We can set this option by constructor or setter:
135
140
136
- ``` php
141
+ ``` php
137
142
$auitolink->textLimit(50);
138
143
139
144
$text = $autolink->convert($text);
@@ -147,19 +152,17 @@ http://campus.asukademy.com/learning/job/84-fin...
147
152
148
153
Use Your own limit handler by set a callback:
149
154
150
- ``` php
151
- $auitolink->textLimit(function($url)
152
- {
155
+ ``` php
156
+ $auitolink->textLimit(function($url) {
153
157
return substr($url, 0, 50) . '...';
154
158
});
155
159
```
156
160
157
161
Or use ` \Asika\Autolink\LinkHelper::shorten() ` Pretty handler:
158
162
159
- ``` php
160
- $auitolink->textLimit(function($url)
161
- {
162
- return \Asika\Autolink\LinkHelper::shorten($url, 15, 6);
163
+ ``` php
164
+ $auitolink->textLimit(function($url) {
165
+ return \Asika\Autolink\Autolink::shortenUrl($url, 15, 6);
163
166
});
164
167
```
165
168
@@ -172,8 +175,8 @@ http://campus.asukademy.com/....../84-find-interns......
172
175
### ` auto_title `
173
176
174
177
Use AutoTitle to force add title on anchor element.
175
-
176
- ``` php
178
+
179
+ ``` php
177
180
$autolink->autoTitle(true);
178
181
179
182
$text = $autolink->convert($text);
@@ -189,7 +192,7 @@ Output:
189
192
190
193
Strip Scheme on link text:
191
194
192
- ``` php
195
+ ``` php
193
196
$auitolink->stripScheme(true);
194
197
195
198
$text = $autolink->convert($text);
@@ -203,9 +206,13 @@ Output
203
206
204
207
### ` escape `
205
208
206
- Strip Scheme on link text:
209
+ Auto escape URL, default is ` true ` :
210
+
211
+ ``` php
212
+ $auitolink->autoEscape(false);
213
+
214
+ $text = $autolink->convert($text);
207
215
208
- ``` php
209
216
$auitolink->autoEscape(true);
210
217
211
218
$text = $autolink->convert($text);
@@ -215,14 +222,15 @@ Output
215
222
216
223
``` html
217
224
<a href =" http://www.google.com.tw?foo=bar&yoo=baz" >http://www.google.com.tw?foo=bar&yoo=baz</a >
225
+ <a href =" http://www.google.com.tw?foo=bar& ; yoo=baz" >http://www.google.com.tw?foo=bar& ; yoo=baz</a >
218
226
```
219
227
220
228
### ` link_no_scheme `
221
229
222
230
Convert URL which no scheme. If you pass ` TRUE ` to this option, Autolink will use
223
231
` http ` as default scheme, you can also provide your own default scheme.
224
232
225
- ``` php
233
+ ``` php
226
234
$auitolink->linkNoScheme('https');
227
235
228
236
$text = $autolink->convert('www.google.com.tw');
@@ -238,9 +246,8 @@ Output
238
246
239
247
You can add new scheme to convert URL begin with it, for example: ` vnc://example.com `
240
248
241
- ``` php
242
- $autolink->addScheme('skype')
243
- ->addScheme('vnc');
249
+ ``` php
250
+ $autolink->addScheme('skype', 'vnc');
244
251
```
245
252
246
253
Default schemes is ` http, https, ftp, ftps ` .
@@ -249,15 +256,10 @@ Default schemes is `http, https, ftp, ftps`.
249
256
250
257
If you don't want to use ` <a> ` element as your link, you can set a callback to build link HTML.
251
258
252
- ``` php
253
- $autolink->setLinkBuilder(function($url, $attribs)
254
- {
259
+ ``` php
260
+ $autolink->setLinkBuilder(function(string $url, array $attribs) {
255
261
$attribs['src'] = htmlspecialchars($url);
256
262
257
- return (string) new \Windwalker\Dom\HtmlElement ('img', null, $attribs);
263
+ return \Asika\Autolink\HtmlBuilder::create ('img', $attribs, null );
258
264
});
259
265
```
260
-
261
- See: [ Windwalker Dom Package] ( https://github.com/ventoviro/windwalker-dom )
262
-
263
-
0 commit comments