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());
Related
Using window.location.hash (used to pass in ID for page) returns something like the following:
Also, for people asking why I used window.location.hash instead of window.location.href is because window.location.href started looping infinitely for some reason, and .hash does not. I don't think this should be a big deal, but let me know if it is and if I need to change it.
http://website.com/NewPage.php#?name=1418019307305
[The string of numbers is actually epoch system time]
When using PHP to try to retrieve this variable It is not picking up any text in the file It's supposed to write to.
<?php
$myfile = fopen("File1.txt","w");
echo $_GET['name'];
fwrite($myfile, $_GET['name']);
fclose($myfile);
?>
Try to print $_SERVER variable and it will give you the array and in the desired key you can get the values. It can help you to find that variable in the string.
If you want to get the value after the hash mark or anchor, that isn't possible with "standard" HTTP as this value is never sent to the server. However, you could parse a URL into bits, including the fragment part, using parse_url().
This should do the trick:
<?php
$name_query = parse_url("http://website.com/NewPage.php#?name=1418019307305");
$get_name = substr($name_query['query'], strpos($name_query['query'], "=") + 1);
echo $get_name;
?>
Working example: http://codepad.org/8sHYUuCS
Then you can use $get_name to store "name" value in a text file.
The hash tag is a fragment that never gets processed by the server, but rather the user-agent, i.e. the browser, so JavaScript may certainly access it. (See https://www.rfc-editor.org/rfc/rfc3986#section-3.5). PHP does allow you to manipulate a url that contains a hash tag with parse_url(). Here's another way to get the info:
<?php
$parts = parse_url("http://website.com/NewPage.php#?name=1418019307305");
list(,$value) = explode("=",$parts['fragment']);
echo $value; // 1418019307305
The placement of the hash tag in this case wipes out the query string so $_SERVER['QUERY_STRING'] will display an empty string. If one were to rewrite the url following best practice, the query string would precede the hash tag and any info following that mark. In which case the script for parsing such a url could be a variation of the preceding, as follows:
<?php
$bestPracticeURL = "http://website.com/NewPage.php?name=1418019307305#more_data";
$parts = parse_url( $bestPracticeURL );
list(,$value) = explode("=", $parts['query']);
$hashData = $parts['fragment'];
echo "Value: $value, plus extra: $hashData";
// Value: 1418019307305, plus extra: more_data
Note how in this case parse_url was able to capture the query string as well as the hash tag data. Of course, if the query string had more than one key and value, then you might need to explode on the '&' into an array and then explode each array element to extract the value.
I was wondering if someone knows what the best method would be to extract a link from another link , Here's an example:
If I have links in the following format:
http://www.youtube.com/watch?v=35HBFeB4jYg OR
http://it.answers.yahoo.com/question/index?qid=20080520042405AApM2Rv OR
https://www.google.it/search?q=rap+tedesco&aq=f&oq=rap+tedesco&aqs=chrome.0.57j62l2.2287&sourceid=chrome&ie=UTF-8#hl=en&sclient=psy-ab&q=migliori+programatori&oq=migliori+programatori&gs_l=serp.3..0i19j0i13i30i19l3.9986.13880.0.14127.14.10.0.4.4.0.165.931.6j4.10.0...0.0...1c.1.7.psy-ab.tPmiWRyUVXA&pbx=1&bav=on.2,or.r_cp.r_qf.&fp=ffc0e9337f73a744&biw=1280&bih=699
How would I go about extracting only the web pages like so:
http://www.youtube.com
http://it.answers.yahoo.com
https://www.google.it
I was wondering if and what regular expression I could use with PHP to achieve this, also are regular expressions the way to go?
There is a PHP function for parsing URLs: parse_url
$url = 'http://it.answers.yahoo.com/question/index?qid=20080520042405AApM2Rv';
$p = parse_url($url);
echo $p["scheme"] . "// . "$p["host"];
Use function parse_url.
$link = "https://www.google.it/search?q=rap+tedesco";
$parseUrl = parse_url($link);
$siteName = $parseUrl['scheme']."://". $parseUrl['host'];
Using Regexp.
preg_match('#http(s?)://([\w]+\.){1}([\w]+\.?)+#',$link,$matches);
echo $matches[0];
Codeviper Demo.
You just want to have the domain of the page, in PHP there exists a function called parse_url that could help
I want to pass a url as a parameter in my PHP script. The URL is coming from my Android app which passes a url and the PHP script custom formats XML to return to the app. So I would pass the url I wanted to format and the PHP script would do stuff and echo the results.
For instance I want to pass this url:
differentwebpage.com/search/?query=ford%20f150&
in my php script (mywebpage.com/script.php):
$url = $_GET['url'];
$str = file_get_contents($url);
echo $str;
So the full url would be:
mywebpage.com/script.php?url=differentwebpage.com/search/?query=ford%20f150&
But this doesn't work. I think I need to URL encode/decode but I haven't had any success. How can I pass a full URL as a parameter?
Try encoding it in Android when you pass it.
You can encode using the URLEncoder here: http://developer.android.com/reference/java/net/URLEncoder.html
You can then easily unencode it in PHP using urldecode.
$url = urldecode($_GET['url']);
$str = file_get_contents($url);
echo $str;
Simply use the php function urlencode(). It will encode the URL in your example like this:
differentwebpage.com%2Fsearch%2F%3Fquery%3Dford%2520f150%26
Which can be fully decoded. And only contains characters that are valid in a URL.
Is there any way to save an XML attribute as a PHP variable and automatically place it in another http request? Or is there a better way to do that?
Basically, I send a server an http request, the code I get back looks something like this:
<tag one="info" two="string">
I need to save the string in attribute two and insert it in an http request that looks something like this:
http://theserver.com/request?method=...&id=123456
The '123456' ID needs to be the string in attribute 'two'.
Any help would be appreciated!
Thanks,
Jane
If you are 100% entirely absolutely completely TOTALLY sure that the content will always have that exact format, you can probably use regex as the other answers have suggested.
Otherwise, DOM isn't very hard to manage...
$dom = new DOMDocument;
$dom->loadXML($yourcontent);
$el = $dom->getElementsByTagName('A')->item(0); // presuming your tag is the only element in the document
if ($el) {
$id = $el->getAttribute('id');
}
$url = 'http://theserver.com/request?method=...&id=' . $id;
If you have a real example of the XML that you'll receive, please do post it and I'll adapt this answer for it.
If you can... send it in JSON. If you can't, and the only thing that is returned is that wee snippet, then I'd use regex to pull out the value.
Something like /.*two="([^"]+)".*/ should match everything, replace matches with '$1'
Otherwise use simplexml.
You could use:
<?php
$string = '<tag one="info" two="123456">';
if (preg_match('/<tag one="[^"]*" two=\"([^"]*)\">/',$string,$match)) {
$url = 'http://theserver.com/request?method=...&id=' . $match[1];
echo $url;
}
?>
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.