Remove Subdomains from Form Post - php

How would I go about extracting just the base URl from a long string that was inputted into a form?
Example:
User inputs: http://stackoverflow.com/question/ask/asdfasneransea
Program needs just: http://stackoverflow.com and not /question/ask/asdfasneransea
What is the easist way to do this using PHP?

You can simply use parse_url()
$url = parse_url('http://example.com/foo/bar');
$host = $url['host']; // example.com

Use the parse_url function to get the separate parts of the URL, then reassemble the parts you are looking for.

Related

Create a php variable from URL without question mark

I need to create a variable in PHP from a URL, which does not have a fully formed query string.
e.g. http://search.domain.com/domain2.com
In this example, the variable needs to be
$website='domain2.com'
Is there a way to convert the entered URL in address bar to my ?website= variable?
An example would be the whois.domaintools service, which allows you to query a whois record from their website using the following url format:
http://whois.domaintools.com/domain.com
This then displays info based on the url you specified.
Can i achieve this using a MOD_Rewrite in the .htaccess, or can i use some PHP function like http_build_query to achieve this? I'm going around in circles and surely missing something obvious!
You can use this code to get your array $urlpart
$link = $_SERVER['REQUEST_URI'];
$urlpart = explode('/',trim(parse_url($link, PHP_URL_PATH), '/'));

Using link data as PHP variable input

I have a link. Eg. abc.com/qwerty. I want to extract the part after / of every input just like examples below and use it just like a PHP GET input value and store it to a variable $page. Essentially, the link abc.com/qwerty should work like abc.com/proc.php?x=qwerty
Typed link Part to be used as PHP GET input
abc.com/cvbx cvbx
abc.com/ghvs ghvx
abc.com/pabc pabc
How can I do this?
You can use: $_SERVER['REQUEST_URI']
$request_uri = $_SERVER['REQUEST_URI']; //returns '/cvbx'
$segments = array_filter(explode('/', $request_uri)); //array_filter to remove empty elements.
You can parse the url as described at http://php.net/manual/en/function.parse-url.php .
If you have urls as described there, you can get the path part with:
$input = substr(parse_url($url, PHP_URL_PATH),1);
substr is to remove the starting /

Extract domain name from affiliate URL using PHP

Here is the format of affiliate URL I have http://tracking.vcommission.com/aff_c?offer_id=2119&&url=http%3A%2F%2Fwww.netmeds.com%2F%3Fsource_attribution%3DVC-CPS-Emails%26utm_source%3DVC-CPS-Emails%26utm_medium%3DCPS-Emails%26utm_campaign%3DEmails
If you see it has 2 URLs:
first URL: is for vcommission.com and
Second URL: netmeds.com
I have CSV file with lot of rows. Each rows may have different second URL. I wanted to get second URL for each rows. First URL is also not static as for different CSV, this would also different.
How can I get second URL?
Some basic string parsing like this should give you an idea.
$url='http://tracking.vcommission.com/aff_c?offer_id=2119&&url=http%3A%2F%2Fwww.netmeds.com%2F%3Fsource_attribution%3DVC-CPS-Emails%26utm_source%3DVC-CPS-Emails%26utm_medium%3DCPS-Emails%26utm_campaign%3DEmails';
list($u,$q)=explode('url=',urldecode($url));
$o=(object)parse_url($q);
echo $o->host;
A good way to find the domain for a URL is with parse_url
Unfortunately due to the way your data is stored this is not really an option however you may be able to use some sort of regex to find contained web addresses in the query string
<?php
$url = "http://tracking.vcommission.com/aff_c?offer_id=2119&&url=http%3A%2F%2Fwww.netmeds.com%2F%3Fsource_attribution%3DVC-CPS-Emails%26utm_source%3DVC-CPS-Emails%26utm_medium%3DCPS-Emails%26utm_campaign%3DEmails";
$p = parse_url($url);
$pattern = "/www[^%]*/";
preg_match($pattern, $p['query'], $result);
var_dump($result);
You may need to adjust the regex pattern based on how the other data presents itself.

parse_url function php

I'm extracting tlds from my urls content with php's parse_url.
than I have an array of top level domains which are compared with the extracted top level domain if they match or not.
$url = parse_url($tag->getAttribute('href'));
if (in_array($url['host'], $affi_urls) || $url['host'] == "www.example.com"){
$tag->setAttribute('href', '/redirect.php?url='.$href);
}
this works fine if the ur['host'] contains the top level domain. if the url['host'] is a relative path than is a big mess overthere.
/redirect.php?url=/example/test
how could I avoid this case?
You need to save the hostname of the page that you're processing. If $url['host'] is empty, use that hostname in its place.
You should encode the url parameters.
$tag->setAttribute('href', '/redirect.php?url='.urlencode($href));
And then after getting the data by parse_url, use urldecode to decode the data.

how to get url after hash

I have a url that looks something like this:
zigzagstudio/#!/page_wedding2
and I need to take the part of the url after the #. In fact I need to reach to page_wedding2 in order to take the number ad compare it with and id from my database. Is this possible using php? Does anyone have an example of code? I also searched for a solution using javascript but I don't know how to send it to php using javascript.
$url = "zigzagstudio/#!/page_wedding2";
$pattern = "([\#\!\/]+(.*))";
preg_match($pattern, $url, $string);
$name = $string[1];
echo $name; // prints 'page_wedding2'
$url = 'zigzagstudio/#!/page_wedding2';
echo parse_url($url, PHP_URL_FRAGMENT);
See parse_url() docs.
You'll need to read it in JavaScript and then pass it to your PHP.
Read it in Javascript like this:
var query = location.href.split('#');
var anchorPart = query[0];
Once you have anchorPart and parsed relevant information from it, pass it to your PHP - there may be different ways of doing this, depending on your web application.
You could make an AJAX request to page_wedding2.php and pass it any parsed information in the querystring. Use the returned HTML string in your own web page.
Edit: To clarify, the browser doesn't pass the anchor part of the URL to the server side.
You can use explode in PHP and split in Javascript.
PHP:
$str = 'zigzagstudio/#!/page_wedding2';
$array = explode('#', $str);
echo array_pop($array);
Javascript:
var str = 'zigzagstudio/#!/page_wedding2';
var str_array = str.split('#');
alert(str_array.pop());

Categories