url-parsing

Break up/parse a URL into its constituent parts in php

江枫思渺然 提交于 2019-12-02 08:52:36
What is the best way to parse a URL into its corresponding paths, such that https://www.example.com/path/to/directory/file.jpeg?param1=foo&param2=bar Results in an array holding Array( ["scheme"] => "https", ["host"] => www.example.com ["directory"] => "path/to/directory" ["filename"] => "file" ["extension] => "jpeg" ["path"] => "path/to/directory/file.jpeg", ["file"] => "file.jpeg" ["params"] => Array( ["param1"] => "foo", ["param2"] => "bar" ) ) Note: The keys do not need to be named like this, they are just an example. I have looked into parse_url, but it doesn't split up the path fine

Parsing an URL in JavaScript

感情迁移 提交于 2019-12-02 07:13:12
问题 I need to parse url in JavaScript. Here is my code: var myRe = /\?*([^=]*)=([^&]*)/; var myArray = myRe.exec("?page=3&Name=Alex"); for(var i=1;i<myArray.length;i++) { alert(myArray[i]); } Unfortunately it only works for first name=value pair. How I can correct my code? 回答1: exec returns a match, or null if no match found. So you need to keep exec ing until it returns null. var myRe = /\?*([^=]*)=([^&]*)/; var myArray; while((myArray = myRe.exec("?page=3&Name=Alex")) !== null) { for(var i=1;i

Parsing an URL in JavaScript

半腔热情 提交于 2019-12-02 05:34:35
I need to parse url in JavaScript. Here is my code: var myRe = /\?*([^=]*)=([^&]*)/; var myArray = myRe.exec("?page=3&Name=Alex"); for(var i=1;i<myArray.length;i++) { alert(myArray[i]); } Unfortunately it only works for first name=value pair. How I can correct my code? exec returns a match, or null if no match found. So you need to keep exec ing until it returns null. var myRe = /\?*([^=]*)=([^&]*)/; var myArray; while((myArray = myRe.exec("?page=3&Name=Alex")) !== null) { for(var i=1;i<myArray.length;i++) { alert(myArray[i]); } } Also, you're regex is wrong, it definately needs th g switch,

Break a URL into its components

巧了我就是萌 提交于 2019-11-29 01:49:55
I'm using javascript and would like to take a URL string that I have and break it down into its components such as the host, path, and query arguments. I need to do this in order to get to one of the query arguments, which is itself a URL and is thus encoded in the original URL string. I feel like there should be an easy way to do this in Javascript. Perhaps something that looks like this: var what_I_Want = url("http://www.domain.com?queryArg1=somequeryargument").getQueryArgumentValue("queryArg1"); Tak The parseUri function will do everything you need Edit Alternatively you can get the DOM to

Is $_SERVER['REQUEST_SCHEME'] reliable?

纵饮孤独 提交于 2019-11-28 20:08:14
I recently was seeking a way to properly determine protocol, under which url request was supplied to the server. I watched through parse_url() and though $_SERVER superglobal variable, and found this: <?php header('Content-Type: text/plain'); print_r($_SERVER); ?> Output: [REQUEST_SCHEME] => http However, I was unable to find it on php.net or Google. Though, I was able to find this question. Q#1: If $_SERVER['REQUEST_SCHEME'] wasn't documented, then it is probably unreliable, or it can be trusted? I'am using VC9 PHP 5.4.14 TS under windows for development. But my production is under ubuntu. Q

Regex ignore URL already in HTML tags

混江龙づ霸主 提交于 2019-11-27 16:15:00
I'm having a little problem with my Regex I've made a custom BBcode for my website, however I also want URLs to be parsed too. I'm using preg_replace and this is the pattern used to identify URLS: /([\w]+:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/is Which works great, however if a URL is within a [img][/img] block, the above pattern also picks it up and produces a result like this: //[img]http://url.com/toimg.jeg[/img] will produce this result: <img src="<a href="http://url.com/toimg.jeg" target="_blank">/> //When it should produce: <img src="http://url.com/toimg.jeg"/> I tried using this: /([^"][\w]+:\/\

Is $_SERVER['REQUEST_SCHEME'] reliable?

心已入冬 提交于 2019-11-27 12:45:31
问题 I recently was seeking a way to properly determine protocol, under which url request was supplied to the server. I watched through parse_url() and though $_SERVER superglobal variable, and found this: <?php header('Content-Type: text/plain'); print_r($_SERVER); ?> Output: [REQUEST_SCHEME] => http However, I was unable to find it on php.net or Google. Though, I was able to find this question. Q#1: If $_SERVER['REQUEST_SCHEME'] wasn't documented, then it is probably unreliable, or it can be

Convert URL parameters to a JavaScript object

好久不见. 提交于 2019-11-25 23:46:08
问题 I have a string like this: abc=foo&def=%5Basf%5D&xyz=5 How can I convert it into a JavaScript object like this? { abc: \'foo\', def: \'[asf]\', xyz: 5 } 回答1: Edit This edit improves and explains the answer based on the comments. var search = location.search.substring(1); JSON.parse('{"' + decodeURI(search).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g,'":"') + '"}') Example Parse abc=foo&def=%5Basf%5D&xyz=5 in five steps: decodeURI: abc=foo&def=[asf]&xyz=5 Escape quotes: same, as

How to get parameters from a URL string?

╄→尐↘猪︶ㄣ 提交于 2019-11-25 22:38:12
问题 I have a HTML form field $_POST[\"url\"] having some URL strings as the value. Example values are: https://example.com/test/1234?email=xyz@test.com https://example.com/test/1234?basic=2&email=xyz2@test.com https://example.com/test/1234?email=xyz3@test.com https://example.com/test/1234?email=xyz4@test.com&testin=123 https://example.com/test/the-page-here/1234?someurl=key&email=xyz5@test.com etc. How can I get only the email parameter from these URLs/values? Please note that I am not getting