I am trying to fetch a variable address from my current URL using JRequest::getVar('address') method.
But if the address value has a (#) character, the part after the # character is not retrieved.
I understand that URI is a combination of query + fragment and the part after a hash symbol is treated as a fragment.
I have tried to use urlencode method but it still doesn't solve the problem.
Can anyone please tell me how to solve the issue?
What is the problem with using urlencode? It should replace # with %23 and all should be well. You can try JRequest::getVar(str_replace('#', '%23', 'address')) which should do the trick. Can you post an example URL that doesn't get properly urlencoded?
I guess you will have to replace the hash-symbol on your own. For example:
str_replace($the_url, '#', '-');
I don't know, where exactly you have to do that, because I don't know how the Joomla!-Framework handles links and urls. But I am sure, that someone else can help here any further...
Encode the Hash in the URL with a %23 replacement
http://twitter.com/home?status=I+believe+in+%23love
"I believe in #love"
The part after # is never sent to Apache/PHP, and can therefore not be retrieved by a PHP script. What you need to do, is to url encode the ADDRESS parameter of the URL.
test.com/index.php?ADDRESS=<?= urlencode('101 Street #6 City') ?>
That code will generate the following url
test.com/index.php?ADDRESS=101+Street+%236+City
Now on this URL, you can retrieve address with JRequest::getVar('ADDRESS')
Check this Joomla doc out. You can retrieve what Joomla call the 'fragment' by doing:
$uri = 'http://fredbloggs:itsasecret#www.example.com:8080/path/to/Joomla/index.php?task=view&id=32#anchorthis';
$u =& JURI::getInstance( $uri );
echo 'Fragment is ' . $u->getFragment();
Related
Ho you all, I've got a script in a Wordpress post that sends the value of 4 variable to a URL.
The fact is that since natively WordPress converts & to &, the URL that is meant to recive those variable cannot get them, since the final URL will be
http://localhost/php/add.php?a=VALUE1&b=VALUE2&c=VALUE3&d=VALUE4
instead of http://localhost/php/add.php?a=VALUE1&b=VALUE2&c=VALUE3&d=VALUE4
Now I know that it is possible to fix this problem by commenting to lines in wp-includes/formatting.php, but I'm looking for a PHP function that can convert the URL with '&' to an URL with just '&'.
Is it possible? Thanks!
You will need to use htmlspecialchars_decode(). Consider this example:
$url = 'http://localhost/php/add.php?a=VALUE1&b=VALUE2&c=VALUE3&d=VALUE4';
$url = htmlspecialchars_decode($url);
echo $url;
// http://localhost/php/add.php?a=VALUE1&b=VALUE2&c=VALUE3&d=VALUE4
I need to pass a url using a GET address. To give an example which was I have tried:
http://www.example.com/area/#http://www.example.com/area2/
I've also tried replacing the forward slashes with other characters but that doesn't seem to work. How would you pass a url in a GET?
As I have understood, you should use url_encode() and url_decode().
The function url_encode() lets you create a string that can be used as a link.
You should use it this way:
$link = 'goto.php?link=' . url_encode($_POST['target_site']);
And when you were going to redirect to the user defined site (eg), you can decode the parameter given this way:
$decoded_link = url_decode($_GET['link']);
// Now it's safe to use the given URL (for example I can redirect to there)
header('location: ' . $decoded_link);
Hope it helps.
The # character links to an anchor on the page. The browser will automatically scroll to the element with the id after the point sign. So that's not what you're looking for.
To pass a GET parameter, the syntax would be like this:
http://example.com/area?http://example.com/area2
Then, if you var_dump($_GET), you'll see your URL. But, if you have other fields you also want to pass in your URL, you can use key=value pairs, like so:
http://example.com/area?url=http://example.com/area2¶m1=a¶m2=b
In this case, your URL will be available in $_GET['url'].
There is a string XX&YY and I'm passing it to another page. ie, localhost/sample/XX&YY/1 for some processing. Now when I try getting the name value on the other side I'm able to get only XX and not full XX&YY. How to rectify it? Any ideas?
Note : here is my url localhost/sample.php?name=somevalue&pageno=somevalue has been url re-written to localhost/sample/name/pageno.
You have to escape the URL . You can use rawurlencode() or urlencode() to encode your URL.
sidenote: Difference of the 2 functions
If I'm understanding correctly, this is the URL to your script:
http://localhost/sample/name/pageno
Which is then rewritten by your web server to this:
http://localhost/sample.php?name=somevalue&pageno=somevalue
Then, this is how you should format the URL:
$url = sprintf('http://localhost/sample/%s/%s',
urlencode('XX&YY'),
urlencode('1')
);
I have been using URL decode on encoded URL variables from $_get.
The current problem I am facing is I have a URL encoded like this:
blah.php?url=http%3A%2F%2Fm.youtube.com%2F#/watch?feature=player_embedded&v=zd7c5tQCs1I&desktop_uri=%2Fwatch%3Fv%3Dzd7c5tQCs1I%26feature%3Dplayer_embedded
I'm not sure what kind of encoding this is, can someone help me? When I use just "urldecode" on this it just returns m.youtube.com
Edit: My problem is not that url decode isn't working, it works if I manually enter this encoded URL and use urldecode(), but when this encoded url is in the actual pages url and I use the _GET function then I try to decode it it stripes off everything after the "#" in the URL.
<?php print urldecode($_GET["url"]);?>
It returns
"http://m.youtube.com/"
instead of
"http://m.youtube.com/#/watch?feature=player_embedded&v=zd7c5tQCs1I&desktop_uri=/watch?v=zd7c5tQCs1I&feature=player_embedded"
I think the issue is that the pound sign is not encoded, if I refresh the page it strips away the pound sing and everything after it, so how do I get around this? Can I still retrieve the info from "GET" even though there is a pound sign? (#)
The problem is that the full link has multiple = signs, and browser cant determine, that the other = signs refer just to the url= parameter.
in your case, at first, you need to use function before link is given to url= parameter:
========================= 1) JAVASCRIPT ======================
<script type="text/javascript">
var mylink = encodeURIComponent('http://testest.com/link.php?name=sta&car=saab');
document.write("http://yoursite.com/url=" + mylink);
</script>
========================= 2)or PHP ===========================
<?php
$mylink = 'http://testest.com/link.php?name=sta&car=saab';
echo 'http://yoursite.com/url='.urlencode($mylink);
?>
so, your output (url parameter) will get like this
http://yoursite.com/url=http%3A%2F%2Ftest.com%2Flink.php%3Fname%3Dsta%
so, the url parameter will get the encoded url.
after that, your .php file needs to decode that "url" parameter-
<?php
$varr = $_GET['url'];
$varr = preg_replace("/%u([0-9a-f]{3,4})/i","&#x\\1;",urldecode($varr));
$varr = html_entity_decode($varr,null,'UTF-8');
echo $varr;
?>
that will give you the correct value
I read on php.net about urldecode function and they say that superglobal $_get is already decoded, ex: "The superglobals $_GET and $_REQUEST are already decoded. Using urldecode() on an element in $_GET or $_REQUEST could have unexpected and dangerous results."
It is encoded into ASCII format .
see http://www.w3schools.com/tags/ref_urlencode.asp
So here is the problem, the pound sign (#) (Hash) wasn't encoded... since I can't go back and re-encode it I have to use javascript (ex. alert(window.location.hash);) to send me the full URL after the hash then I append it to PHP's version of the URL, I THEN use a find and replace function in PHP to replace the "#" with "%23", then I use the urldecode method and it returns the full proper url decoded.
This encoding is called percent encoding or URL encoding. You can use urldecode for decoding it. (Example: http://phpfiddle.org/lite/code/0nj-198 )
I am using a script to check links on a given page. I am using simple html DOM to parse the information into an array. I have to check the href of all the a tags to find if they contain a file or something like # or JS.
I tried the following without success.
if(preg_match("|^(.*)|iU", $href)){
save_link();
}
I dont know it my pattern is wrong or if there is a better method to complete this function.
I want to be able to detect if $href contains .com .php .file extensions. This way it will filter out items like # "function()" and other items used in the href attribute.
EDIT:
parse_url will not work stop posting it. The value # returns as a valid url like I stated above I am trying to look for any string followed by .* with no more than 4 chars following the .
I believe that the function you're looking for is parse_url().
This function will take a URL string, and return an array of components, which will allow you to work out what kind of URL it is.
However note that it has issues with incomplete URLs in PHP versions prior to 5.4.7, so you need to have the very latest PHP to get the best out of it.
Hope that helps.
See http://php.net/manual/en/function.parse-url.php
I'm assuming you don't want to match fragments (#) because you are not concerned with following internal anchors.
parse_url breaks up the different parts of the url into an array. You can see the path component of the URL in this array and run your check against that.
You can use parse_url() , like this :
$res = parse_url($href);
if ( $res['scheme'] == 'http' || $res['scheme'] == 'https'){
//valid url
save_link();
}
UPDATE:
I've added code to filter only http and https urls, thanks to Baba for spotting this.