I have this domain
https://test.com/?url=https://google.com/search?q=#ie7&rls=login.microsoft:en-US:IE-Address&ie=&oe=#
And this is the code:
<?php
//check if the url parameter exists
if(isset($_GET['url'])) $url=$_GET['url'];
else $url=FALSE;
?>
If I use this in the html
<?=(!$url) ? '' : $url ?>
I get an output that cuts off in special characters like # or & etc.. and becomes like this for example https://google.com/search?q=# I tried urlencode/decode but couldn't figure it out
if you need all after ?url=, you can try this:
$url = $_SERVER['REQUEST_URI'];
$url2 = substr($url, strpos($url, '?url')+5);
$url = substr($url, strpos($url, '?url'));
print($url); // url?=https://..etc
print($url2); // https://..etc
you want to output the full domain just after
https://test.com/?url=
to the end, and get https://google.com/search?q=#ie7&rls=login.microsoft:en-US:IE-Address&ie=&oe=# right?
So if this is fixed, you can do:
echo substr($url, 22);
Related
I am going to make a URL checking system.
I have this URL
https://lasvegas.craigslist.org/mob/6169799901.html
Now I want to make this URL like this
https://lasvegas.craigslist.org/search/mob?query=6169799901
how can I do it using PHP?
Since I ended up (maybe?) solving it anyways, here's one method using URL/path parsing:
$url = 'https://lasvegas.craigslist.org/mob/6169799901.html';
$parsed = parse_url($url);
$basepath = pathinfo($parsed['path']);
echo $parsed['scheme'].
"://".
$parsed['host'].
"/search".
$basepath['dirname'].
"?query=".
$basepath['filename'];
Formatted for readability.
https://3v4l.org/E6Y54
Try this
$url = "https://lasvegas.craigslist.org/mob/6169799901.html";
$id = substr($url, strrpos($url, '/') + 1);
$id = str_replace(".html","",$id);
$result = "https://lasvegas.craigslist.org/search/mob?query=".$id;
echo $result;
code:
<?php
$query = "select * from latest_news limit 0,10";
$fet = mysqli_query($link,$query);
while ($fetch = mysqli_fetch_array($fet))
{
?>
<p id="news-h3"><?php echo $fetch['news_title']; ?>...[Read More]</p>
<?php
}
?>
When I click on link i.e.
[Read More]
It show
best%20engineering%20college%20in%20India
Here, I want to replace %20 with (-) from url and get result like
best-engineering-college-in-India
how can I fix this problem please help.
Thank you
1.you can use this php built in function.you just want to replace a piece of string by another.
$new_url = str_replace('%20', '-', $url);
for more information
http://fr2.php.net/str_replace
2.Use urlencode to encode. The decoding will happen automatically,
and if you want to remove total 20% then use this code.
$url = "one%20%26%20two";
$a = urldecode($url); // -> "one & two"
suppose your page when loading then create your url like this
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$base_url = $protocol . $_SERVER["SERVER_NAME"];
then use it
urldecode($base_url)
if you want to use this in url then
echo '<a href="?mycgi?foo=', urlencode($userinput), '">';
for more information
http://www.php.net/manual/en/function.urldecode.php
You should use str_replace() here:
echo str_replace('%20', '-', $input); // best-engineering-college-in-India
Basically you want to decode the URL. You can decode it using
echo utf8_decode(urldecode($input));
I'm trying to check the string after the last trailing slash in my URL.
My code is as follows:
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$data = substr($url, strrpos($url, '/') + 1);
if($data == "dashboard") {
require_once VIEW_ROOT . '/cp/dashboard_view.php';
} else {
echo $data;
}
Once I go to http://MYURL/dashboard/in it should show in as the $data. Instead it gives me a 500 error.
You can simply use explode() function to break the string... .Or else $_SERVER[REQUEST_URI] shall give you the data after the host name...
But for the data after the last '/' explode function will work the best..
This will work.
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$x = explode('/',$url);
$data = $x[sizeof($x)-1];
echo $data;
You should try :
$url = "http://".$_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
You need to join
http:// string with $_SERVER[HTTP_HOST] and then $_SERVER[REQUEST_URI] using .(dot).
This question already has answers here:
Get only filename from url in php without any variable values which exist in the url
(13 answers)
Closed 7 years ago.
I wanted to print the last characters after "/" in a url. but instead it is printing the whole url, I expected the output to be just "index.php" instead it is printing out the whole url.
How should i go about doing it right?
$data = $_SERVER['REQUEST_URI'];
$whatIWant = substr($data, strpos($data, "/") + 1);
echo $whatIWant;
You can see it here
You should get the actual link by
<?php
$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$getpath=explode("/",$actual_link);
echo end($getpath);
?>
Short Explanation :
Step 1 : Get the url by
http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]
Step 2 : Explode with slash
explode("/",$actual_link)
Step 3 : Get the last part
end($getpath);
Try this..
<?php
$data = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$whatIWant = explode("/",$data);
echo end($whatIWant);?>
You can also try it this way using strrchr :
$url = 'http://spiritofethiopia.com/test/test/test/index.php';
$str = substr(strrchr($url, '/'), 1);
echo $str;
strrchr — Find the last occurrence of a character in a string.
You can use preg_match whith a correct RegExp to capture the end of the URL.
<?php
$data = $_SERVER['REQUEST_URI'];
if(preg_match('#/([^/]*?)$#', $data, $matches) == 1) {
echo $matches[1];
}
else {
// Should not happen
/*
* Throw exception
*/
}
?>
Try strripos instead of strpos, it may works
<?php
$data = $_SERVER['REQUEST_URI'];
$whatIWant = substr($data, strripos($data, "/") + 1);
echo $whatIWant;
?>
Try This:
working solution,
<?php
$url = 'http://test/test/test/index.php';
$tokens = explode('/', $url);
echo $tokens[sizeof($tokens)-1];
?>
I'm trying to change a value in a string that's holding my current URL. I'm trying to get something like
http://myurl.com/test/begin.php?req=&srclang=english&destlang=english&service=MyMemory
to look like
http://myurl.com/test/end.php?req=&srclang=english&destlang=english&service=MyMemory
replacing begin.php for end.php.
I need the end.php to be stored in a variable so it can change, but begin.php can be a static string.
I tried this, but it didn't work:
$endURL = 'end.php';
$beginURL = 'begin.php';
$newURL = str_ireplace($beginURL,$endURL,$url);
EDIT:
Also, if I wanted to replace
http://myurl.com/begin.php?req=&srclang=english&destlang=english&service=MyMemory
with
http://newsite.com/end.php?req=&srclang=english&destlang=english&service=MyMemory
then how would I go about doing that?
Assuming that you want to replace the script filename of the url, you can use something like this :
<?php
$endURL = 'end.php';
$url ="http://myurl.com/test/begin.php?req=&srclang=english&destlang=english&service=MyMemory";
$pattern = '/(.+)\/([^?\/]+)\?(.+)/';
$replacement = '${1}/'.$endURL.'?${3}';
$newURL = preg_replace($pattern , $replacement, $url);
echo "url : $url <br>";
echo "newURL : $newURL <br>";
?>
How do you want them to get to end.php from beigin.php? Seems like you can just to a FORM submit to end.php and pass in the variables via POST or GET variables.
The only way to change what page (end.php, begin.php) a user is on is to link them to another page from that page, this requires a page refresh.
I recently made a PHP-file for this, it ended up looking like this:
$vars = $_SERVER["QUERY_STRING"];
$filename = $_SERVER["PHP_SELF"];
$filename = substr($filename, 4);
// for me substr removed 'abc/' in the beginning of the string, you can of course adjust this variable, this is the "end.php"-variable for you.
if (strlen($vars) > 0) $vars = '?' . $vars;
$resultURL = "http://somewhere.com" . $filename . $vars;