PHP - How to get last segment of URL string [duplicate] - php

This question already has answers here:
Can I read the hash portion of the URL on my server-side application (PHP, Ruby, Python, etc.)?
(12 answers)
Closed 9 years ago.
I'm trying to extract the last segment of this URL: http://test.com/blog/#segmentIwant (the #segmentIwant is the string I want). The hash tag is generated from a link within the website. Any help is much appreciated!

PHP cannot read the hash, the server doesnt even recieve it at all!

Well while you load a page with # tag, php does not know it as # tags on url are treated by basically javascript and are not sent to server. But with a simple AJAX request you can send the value to server on loading the page and get the response through that AJAX response.
To grab the url with Javascript:
var url = document.URL;
And try basename() function to get the last part of the sent url with PHP code.
echo basename( "http://test.com/blog/#segmentIwant" );
Or a simple function in javascript to do it:
var url = document.URL;
hashed_string = url.split("#");
alert(hashed_string[1]);

Related

$.getJSON PHP alternative [duplicate]

This question already has answers here:
Get JSON object from URL
(11 answers)
Closed 2 years ago.
$.getJSON might be misleading, but what I want to do is get JSON from a url via a PHP file.
var url = "https://script.google.com/macros/s/XXXXXXXXXX/exec?action=read";
$.getJSON(url, function (json) {
//DO SOMETHING WITH THE DATA
}
This works fine in javascript. When I google get JSON from url maybe it's because I'm learning PHP but I can't seem to find an answer that works. Or when I google $.getJSON I get people trying to get Json from a PHP file.
All my data is stored via a googlesheet that I access by calling google script and it sends it back and I can take the information I want e.g
json.records[0].NAME;
I want to pass some data to a PHP file that I can't fathom how to do that.
Any help would be greatly appreciated.
It's simple as js:
$content = file_get_contents('https://script.google.com/macros/s/XXXXXXXXXX/exec?action=read');
$data = json_decode($content); // return object
$data = json_decode($content, true); // return array

how to get #jumper part from url in codeigniter? [duplicate]

This question already has answers here:
Can I read the hash portion of the URL on my server-side application (PHP, Ruby, Python, etc.)?
(12 answers)
Closed 7 years ago.
My Url is
http://www.domain.com/seg_one/seg_two/seg_three#jumper
I want to get #jumper part of current url
In JavaScript, you can get this by simply location.hash property of window object. i.e.
window.location.hash; // will give you #jumper.
From here once you have it on the client side, do anything you want with it. You can send it back to server by even making an ajax call.
The # is called a fragment. The problem is that browsers won't transmit those to the server so there is now way to parse it.
You can get it via javascript (see below) and make an ajax request to use it in the back-end(PHP):
window.location.href
You can condition the ajax call:
address = window.location.href;
index = address.str.indexOf("#");
if(typeof index !='null') {
var term = str.slice(address.str.indexOf("#")+1, address.length);
console.log(term);//will display jumper
//send it via AJAX
}
$third = $this->uri->segment(3);
$thirdArr = explode('#', $third);
$hash = $thirdArr[1];

How to get fragment ID from URL in PHP? [duplicate]

This question already has answers here:
Can I read the hash portion of the URL on my server-side application (PHP, Ruby, Python, etc.)?
(12 answers)
Closed 5 years ago.
I'm using
location.hash = $(this).attr("id");
to append id into url bar immediately without reloading. the outcome is : http://example.net/index.php/abc#id
then I echo
$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
the output I got was : http://example.net/index.php/abc
How can I get the #id (known as fragment id ) in PHP so that it can be used as PHP variable ?
Thank you!
The webserver don't receive the hash fragment, it's client side only. So you cannot get it with PHP or any other server side language. You can hack around by fetching that hash via javascript and sending it via a GET parameter to the server side script but i don't recommend this.
You can not directly send the fragment part from client to the PHP server side. But the fragment id can be accessed through javascript and you can send it to server as a GET parameter.
Use window.location to get the full url including fragment part and window.location.hash to get just the fragment part in javascript.
In case you send the full url (including fragment hash) to the PHP server, you can use parse_url("URL HERE",PHP_URL_FRAGMENT) function to get the fragment hash.

php variable inside js code [duplicate]

This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 8 years ago.
I need a php variable inside js code.
Why this doens't work?
index.php
<?php
$h = 966;
?>
index.js
var b = <?php echo $h;?>;
Error:
Uncaught SyntaxError: Unexpected token <
You can't run php inside a JavaScript (.js) file.
Take a look at the question shared by Ed Cottrell in the comment on your post for more information, but basically, PHP is a server language, whereas JavaScript is a client one—when your computer loads index.js, it sees the php script, but has no idea what to do with it.
A straightforward solution (though not the best) would be to output a <script> tag containing your variable inside from your php file— and then access it from your JavaScript file.
When your browser requests the Js file, the web server typically simply sends it the file in the response without processing it like a php file. It doesn't know you have php code in it. It does the same (just sends it to the requesting browser) for images, text files, etc.
You can try doing that in your php view/template/script (I would wrap the output in single quotes). Something like:
<script>var b = '<?php echo $h;?>';</script>
You could also make a JavaScript file with a php extension: index.js.php, and include this instead of index.js. You'll need to set some headers in the top of that file to let the browser know it's getting JavaScript:
header("content-type: application/javascript");
There is some slightly more advanced stuff you can do as well: use an htaccess file to parse certain requests (like for that Js file) using php, or using an asynchronous request (Ajax) to pull the value of 'b' from the server, but it looks like your doing something a bit more simple.

How can I parse html that includes javascript code [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Server side browser that can execute JavaScript
Execute javascript in PHP
How can I parse HTML that includes JavaScript code, preferably with PHP script if possible.
As an example:
link
should be replaced by the appropriate value the JavaScript function returns, e.g.
link
A more complex example would be a saved facebook html page which is littered with loads of javascript code.
Summarized:
Return a DOM for a page with html+javascript
You could just give this link an ID. Not that this solution is javascript, jQuery.
So give the link an Id, or class.
$('.link').each(function() {
var functionName = 'link';
var start = $(this).attr('href');
remove = start.replace('javascript:', ''),
get = remove.replace(new RegExp('^'+functionName+'\(((.+\,?)+)\)\;?', 'g'), function(a, b, c) {
return c.replace(/[()']/g,'')
}), args = get.split(",");
//read settings
var firstArgument = args[0];
$(this).attr('href', firstArgument)
});​
Please note this is just an example.
Usage:
​<a class="link" href="javascript:link('http://facebook.com')"​>Hi</a>​​​​​​​​​​​​​​​​​​
That would make the actual link http://facebook.com.
Adding new arguments this way is difficult though and its not really professional.
But this should do what you want, I just didn't know what your link function actually doesnt so I didnt add the argument with the boolean. Of course this could get far more complex and you could write a function that could do this too but I just wrote this for your really quick.
Check out the example.
You can't. When clicked, javascript: URIs just call the function and let it do whatever it wants (which MIGHT include navigating somewhere); they don't expect or use a return value. In many use cases, the function may not cause any navigation at all.
DomDocument can be used to parse HTML in PHP including JS: http://php.net/manual/en/class.domdocument.php
You can "render" the JS with the HTML by merely echoing out the output of a cURL or wget (or whatever you use) without escaping the HTML characters. For external JS you are going to need to build a crawler which will crawl the DomDocument script tags and fetch the appropiate URL and load it into a position that is accessible unless you use it directly from their servers but I don't think they will be too happy about that.
Edit: My new answer after some comments is: no

Categories