PHP search a string for http:// or www [duplicate] - php

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Detecting a url using preg_match? without in the string
I wanting to search a string that is input by the user, for instances of http:// if one is found I want to wrap the link in the correct html,
so for example, the following could be input by a user,
Google's url is http://www.google.com,
what I want to save that as it,
Google's url is http://www.google.com
is this possible?

function make_clickable($sText) {
$sClickText = str_replace(' www.', ' http://www.', $sText);
$sClickText = preg_replace("/([\s])?(http|ftp|https)([\:\/\/])([^\s]+)/i", " $2$3$4",$sClickText);
return $sClickText;
}
Tadaa!

Related

PHP Get Twitter username from URL (Regex) [duplicate]

This question already has answers here:
PHP - parse current URL
(6 answers)
Closed 2 years ago.
I'm looking to be able to extract a Twitter username from a URL.
E.g: https://twitter.com/jack => jack
I found this Regex to be helpful.
if (preg_match("/^https?:\/\/(www\.)?twitter\.com\/(#!\/)?(?<name>[^\/]+)(\/\w+)*$/", $url, $regs)) {
return $regs['name'];
}
It doesn't seem to work when twitter URL contains query parameters.
For example = https://twitter.com/jack?lang=en returns jack?lang=en
Any idea how to improve the regex to prevent this ?
preg_match('/https?:\/\/twitter\.com\/(?<name>[^\?]+)\??.*/', 'https://twitter.com/jack?lang=en', $m);
var_dump(trim($m['name']));
$path = parse_url('https://twitter.com/jack?lang=en',PHP_URL_PATH);
var_dump(str_replace('/','', $path));
string(4) "jack"

replace particular value from url [duplicate]

This question already has answers here:
PHP, Delete parts of a URL variable [duplicate]
(3 answers)
Closed 4 years ago.
I want to replace '/50' with URL.my URL is
http://localhost/CI/admin/pcustomer/cprofile/50?customer=18267&mobile=&nearby=
I want url as
http://localhost/CI/admin/pcustomer/cprofile?customer=18267&mobile=&nearby=
From that little info in your question, this seems to be the simplest way to achieve "I want to replace '/50' with URL"
$url = str_replace('/50', '', 'http://localhost/CI/admin/pcustomer/cprofile/50?customer=18267&mobile=&nearby=');

How can I get URL (with some parameters)? [duplicate]

This question already has answers here:
Get URL query string parameters
(11 answers)
Closed 6 years ago.
I have a form on page, with url like this:
site.com/form.index.html?ref=https://login....
And I get page URL via this code:
http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]
But as result I get not full URL, just this:
site.com/form.index.html
How can I get full URL?
You want to add the query string:
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]$_SERVER[QUERY_STRING]";
Additionally, you may replace the http bit with $_SERVER[REQUEST_SCHEME]:
"$_SERVER[REQUEST_SCHEME]://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]$_SERVER[QUERY_STRING]"

Extract site link from google URL [duplicate]

This question already has answers here:
How can I get parameters from a URL string?
(12 answers)
Closed 8 years ago.
I want to extract site link from Google URL, I need an efficient way to do this,
I have extracted this, but i am not comfortable with that like,
$googleURL = "http://www.google.ca/local_url?dq=food+Toronto,+ON&q=https://plus.google.com/110334461338830338847/about%3Fgl%3DCA%26hl%3Den-CA&ved=0CHAQlQU&sa=X&ei=HzrCVNX-JqSzigb-94D4CQ&s=ANYYN7nQx_FiR1PuowDmXBi1oyfkI2MImg";
I want this
https://plus.google.com/110334461338830338847/
I have done this in a following way.
$first = current(explode("about", $googleURL)); // returns http://www.google.ca/local_url?dq=food+Toronto,+ON&q=https://plus.google.com/110334461338830338847/
and then,
$myLink = explode("&q=", $first);
echo $myLink[1]; // return my need => https://plus.google.com/110334461338830338847/
but there may be two "about" or "&q=" in a googleURL which can cause problem.
I know that, this googleURL will be redirected to my need, but I need that specific link for a purpose.
I guess that it is not really safe to parse that since google can change its implementation anytime.
However, if you want to get a parameter from a String url, this question covers it pretty well :
How to get parameters from a URL string?
$parts = parse_url($googleUrl);
parse_str($parts['query'], $query);
echo $query['q'];

Building <a></a> when user enter an url [duplicate]

This question already has answers here:
Replace URLs in text with HTML links
(17 answers)
Closed 9 years ago.
I have a input field which user enters something. Basically when user enter an url which start with http:// https// or www. i want to store it like user's words...
I'm working with php and mysql. I think replace function is for it. But when I read about it, this function only working on all string as far as I know.
For example:
input:
Hi, how are you? Please check this link : http:// www.google.com and enjoy.
output should like below:
Hi, how are you? Please check this link : http://www.google.com and enjoy.
Try this: (I don't test this code)
$output = preg_replace("#(http://|www\.)(.*) #Uis",'$1$2', $input);

Categories