parse-url

Running parse_url() on a string that may not contain the protocol

谁说我不能喝 提交于 2019-12-31 03:33:26
问题 I'm trying to get the domain name and TLD (no subdomain) from a user-input URL string which may or may not have protocol, directories, subdomains, filenames, etc. In other words, given any of the following: example.com www.example.com sub.example.com example.com/whatever/hey.html http://example.com https://subdomain.example.com ftp://example.com/whatever/hey.html I should always end up with: example.com . Right now this is what I am doing: $hostParts = explode('.', parse_url($URL, PHP_URL

getting youtube video id the PHP

折月煮酒 提交于 2019-12-30 10:17:22
问题 I am currently writing a webapp in which some pages are heavily reliant on being able to pull the correct youtube video in - and play it. The youtube URLS are supplied by the users and for this reason will generally come in with variants one of them may look like this: http://www.youtube.com/watch?v=y40ND8kXDlg while the other may look like this: http://www.youtube.com/watch/v/y40ND8kXDlg Currently I am able to pull the ID from the latter using the code below: function get_youtube_video_id(

parse_url() returns an error when example.com is passed

…衆ロ難τιáo~ 提交于 2019-12-14 04:18:14
问题 According to following code if $host_name is something like example.com PHP returns a notice: Message: Undefined index: host but on full URLs like http://example.com PHP returns example.com . I tried if statements with FALSE and NULL but didn't work. $host_name = $this->input->post('host_name'); $parse = parse_url($host_name); $parse_url = $parse['host']; How can I modify the script to accept example.com and return it? 回答1: Upgrade your php. 5.4.7 Fixed host recognition when scheme is

php parse_url reverse — parsed url

拥有回忆 提交于 2019-12-08 14:27:57
问题 Is there a way to reverse the url from a parsed url? $url = 'http://www.domain.com/dir/index.php?query=blabla#more_bla'; $parse = parse_url($url); print_r($parse); /* array( 'scheme'=>'http://', etc.... ) */ $revere = reverse_url($parse); // probably does not exist but u get the point echo $reverse; //outputs:// "http://www.domain.com/dir/index.php?query=blabla#more_bla" Or if there is a way validate a url that is missing part of its recommended urls e.g www.mydomain.com mydomain.com should

PHP: Remove 'WWW' from URL inside a String

戏子无情 提交于 2019-11-27 15:57:48
问题 Currently I am using parse_url, however the host item of the array also includes the 'WWW' part which I do not want. How would I go about removing this? $parse = parse_url($url); print_r($parse); $url = $parse['host'] . $parse['path']; echo $url; 回答1: $url = preg_replace('#^www\.(.+\.)#i', '$1', $parse['host']) . $parse['path']; This won't remove the www in www.com , but www.www.com results in www.com . 回答2: preg_replace('#^(http(s)?://)?w{3}\.#', '$1', $url); if you don't need a protocol

Parse an URL in JavaScript

我的未来我决定 提交于 2019-11-26 10:24:42
How do I parse an URL with JavaScript (also with jQuery)? For instance I have this in my string, url = "http://example.com/form_image_edit.php?img_id=33" I want to get the value of img_id I know I can do this easily with PHP with parse_url() , but I want to know how it is possible with JavaScript. Sindre Sorhus You can use a trick of creating an a -element, add the url to it, and then use its Location object . function parseUrl( url ) { var a = document.createElement('a'); a.href = url; return a; } parseUrl('http://example.com/form_image_edit.php?img_id=33').search Which will output: ?img_id

Parse an URL in JavaScript

眉间皱痕 提交于 2019-11-26 02:32:57
问题 How do I parse an URL with JavaScript (also with jQuery)? For instance I have this in my string, url = \"http://example.com/form_image_edit.php?img_id=33\" I want to get the value of img_id I know I can do this easily with PHP with parse_url() , but I want to know how it is possible with JavaScript. 回答1: You can use a trick of creating an a -element, add the url to it, and then use its Location object. function parseUrl( url ) { var a = document.createElement('a'); a.href = url; return a; }