How to always add http protocol on links with php?
I have a function to create html links from text in php.
I'd like to always add http:// before links.
function toLink($titulo){
$url = '~(?:(https?)://([^s<]+)|(www.[^s<]+?.[^s<]+))(?<![.,:])~i';
$titulo = preg_replace($url, '<a href="$0" target="_blank" title="$0">$0</a>', $titulo);
return $titulo;
}
and:
$titulo = toLink("some text: www.example.com");
I'd like this url to be convert to <a href="http://www.example.com">
instead of <a href="www.example.com">
.
In other words, I ALWAYS would like to have http on my links. If the text url has http or not. Any ideas how to always have http at the begining of the url?
thanks.
php
add a comment |
I have a function to create html links from text in php.
I'd like to always add http:// before links.
function toLink($titulo){
$url = '~(?:(https?)://([^s<]+)|(www.[^s<]+?.[^s<]+))(?<![.,:])~i';
$titulo = preg_replace($url, '<a href="$0" target="_blank" title="$0">$0</a>', $titulo);
return $titulo;
}
and:
$titulo = toLink("some text: www.example.com");
I'd like this url to be convert to <a href="http://www.example.com">
instead of <a href="www.example.com">
.
In other words, I ALWAYS would like to have http on my links. If the text url has http or not. Any ideas how to always have http at the begining of the url?
thanks.
php
Your RegExp is explicitly stripping the protocol ... why?
– CD001
Jan 18 at 16:24
@CD001 I don't know, I get this regex in another topic.
– Rick Joe
Jan 18 at 16:25
1
Well - it's not explicitly stripping it, it's just format matching and happily accepting it with or without the protocol - it's not really designed for what you're attempting.
– CD001
Jan 18 at 16:26
@CD001 any ideas how to transform text links into links with http at the beggining?
– Rick Joe
Jan 18 at 16:27
1
I might consider using parse_url and checking to see if the protocol is there and, if not, prefixing it ... though you'd need to know whether the destination was supposed to behttp
orhttps
– CD001
Jan 18 at 16:28
add a comment |
I have a function to create html links from text in php.
I'd like to always add http:// before links.
function toLink($titulo){
$url = '~(?:(https?)://([^s<]+)|(www.[^s<]+?.[^s<]+))(?<![.,:])~i';
$titulo = preg_replace($url, '<a href="$0" target="_blank" title="$0">$0</a>', $titulo);
return $titulo;
}
and:
$titulo = toLink("some text: www.example.com");
I'd like this url to be convert to <a href="http://www.example.com">
instead of <a href="www.example.com">
.
In other words, I ALWAYS would like to have http on my links. If the text url has http or not. Any ideas how to always have http at the begining of the url?
thanks.
php
I have a function to create html links from text in php.
I'd like to always add http:// before links.
function toLink($titulo){
$url = '~(?:(https?)://([^s<]+)|(www.[^s<]+?.[^s<]+))(?<![.,:])~i';
$titulo = preg_replace($url, '<a href="$0" target="_blank" title="$0">$0</a>', $titulo);
return $titulo;
}
and:
$titulo = toLink("some text: www.example.com");
I'd like this url to be convert to <a href="http://www.example.com">
instead of <a href="www.example.com">
.
In other words, I ALWAYS would like to have http on my links. If the text url has http or not. Any ideas how to always have http at the begining of the url?
thanks.
php
php
asked Jan 18 at 16:16
Rick JoeRick Joe
1,255827
1,255827
Your RegExp is explicitly stripping the protocol ... why?
– CD001
Jan 18 at 16:24
@CD001 I don't know, I get this regex in another topic.
– Rick Joe
Jan 18 at 16:25
1
Well - it's not explicitly stripping it, it's just format matching and happily accepting it with or without the protocol - it's not really designed for what you're attempting.
– CD001
Jan 18 at 16:26
@CD001 any ideas how to transform text links into links with http at the beggining?
– Rick Joe
Jan 18 at 16:27
1
I might consider using parse_url and checking to see if the protocol is there and, if not, prefixing it ... though you'd need to know whether the destination was supposed to behttp
orhttps
– CD001
Jan 18 at 16:28
add a comment |
Your RegExp is explicitly stripping the protocol ... why?
– CD001
Jan 18 at 16:24
@CD001 I don't know, I get this regex in another topic.
– Rick Joe
Jan 18 at 16:25
1
Well - it's not explicitly stripping it, it's just format matching and happily accepting it with or without the protocol - it's not really designed for what you're attempting.
– CD001
Jan 18 at 16:26
@CD001 any ideas how to transform text links into links with http at the beggining?
– Rick Joe
Jan 18 at 16:27
1
I might consider using parse_url and checking to see if the protocol is there and, if not, prefixing it ... though you'd need to know whether the destination was supposed to behttp
orhttps
– CD001
Jan 18 at 16:28
Your RegExp is explicitly stripping the protocol ... why?
– CD001
Jan 18 at 16:24
Your RegExp is explicitly stripping the protocol ... why?
– CD001
Jan 18 at 16:24
@CD001 I don't know, I get this regex in another topic.
– Rick Joe
Jan 18 at 16:25
@CD001 I don't know, I get this regex in another topic.
– Rick Joe
Jan 18 at 16:25
1
1
Well - it's not explicitly stripping it, it's just format matching and happily accepting it with or without the protocol - it's not really designed for what you're attempting.
– CD001
Jan 18 at 16:26
Well - it's not explicitly stripping it, it's just format matching and happily accepting it with or without the protocol - it's not really designed for what you're attempting.
– CD001
Jan 18 at 16:26
@CD001 any ideas how to transform text links into links with http at the beggining?
– Rick Joe
Jan 18 at 16:27
@CD001 any ideas how to transform text links into links with http at the beggining?
– Rick Joe
Jan 18 at 16:27
1
1
I might consider using parse_url and checking to see if the protocol is there and, if not, prefixing it ... though you'd need to know whether the destination was supposed to be
http
or https
– CD001
Jan 18 at 16:28
I might consider using parse_url and checking to see if the protocol is there and, if not, prefixing it ... though you'd need to know whether the destination was supposed to be
http
or https
– CD001
Jan 18 at 16:28
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54257739%2fhow-to-always-add-http-protocol-on-links-with-php%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54257739%2fhow-to-always-add-http-protocol-on-links-with-php%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Your RegExp is explicitly stripping the protocol ... why?
– CD001
Jan 18 at 16:24
@CD001 I don't know, I get this regex in another topic.
– Rick Joe
Jan 18 at 16:25
1
Well - it's not explicitly stripping it, it's just format matching and happily accepting it with or without the protocol - it's not really designed for what you're attempting.
– CD001
Jan 18 at 16:26
@CD001 any ideas how to transform text links into links with http at the beggining?
– Rick Joe
Jan 18 at 16:27
1
I might consider using parse_url and checking to see if the protocol is there and, if not, prefixing it ... though you'd need to know whether the destination was supposed to be
http
orhttps
– CD001
Jan 18 at 16:28