How to get string after second slash in url - using php? - php

How to get string after second slash in url? URL is different every time (more slashes), but every time I need the whole text after the second slash. How to do it?
I am using this code:
<?php
$str = "$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$last = substr($str, strrpos($str, '/') - 1);
echo $last;
?>
...but it works online with some characters after slash.
Thank you very much for help.

$last = explode("/", $str, 3);
echo $last[2];

<?php
$str = "google.com/whatever/hello";
$last = GetStringAfterSecondSlashInURL($str);
echo $last;
function GetStringAfterSecondSlashInURL($the_url)
{
$parts = explode("/",$the_url,3);
if(isset($parts[2]))
return $parts[2];
}
?>

use
<?php
$str = "$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$last = explode("/",$str,3);// so at second index rest of the string will come.
echo $last[2];
see here http://www.w3schools.com/php/func_string_explode.asp

Related

How to strip text from string if contained

I have a string $current_url that can contain 2 different values:
http://url.com/index.php&lang=en
or
http://url.com/index.php&lang=jp
in both cases I need to strip the query part so I get: http://url.com/index.php
How can I do this in php?
Thank you.
Simplest Solution
$url = 'http://url.com/index.php&lang=en';
$array = explode('&', $url);
echo $new_url =$array[0];
To only remove the lang query do this
$url = 'http://url.com/index.php&lang=en&id=1';
$array = explode('&lang=en', $url);
echo $new_url = $array[0] .''.$array[1];
//output http://url.com/index.php&id=1
So this way it only removes the lang query and keep other queries
If the value of your lang parameter is always of length 2, which should be the case for languages, you could use:
if(strpos($current_url, '&lang=') !== false){
$current_url = str_replace(substr($current_url, strpos($current_url, '&lang='), 8), '', $current_url);
}
If the substring "&lang=" is present in $current_url, it removes a substring of length 8, starting at the "&lang=" position. So it basically removes "&lang=" plus the 2 following chars.
You can Use strtok to remove the query string from url.
<?php
echo $url=strtok('http://url.com/index.php&lang=jp','&');
?>
DEMO
Answer based on comment.
You can use preg_replace
https://www.codexworld.com/how-to/remove-specific-parameter-from-url-query-string-php/
<?php
$url = 'http://url.com/index.php?page=site&lang=jp';
function remove_query_string($url_name, $key) {
$url = preg_replace('/(?:&|(\?))' . $key . '=[^&]*(?(1)&|)?/i', "$1", $url_name);
$url = rtrim($url, '?');
$url = rtrim($url, '&');
return $url;
}
echo remove_query_string($url, 'lang');
?>
DEMO

break and get the contents of an url using php

www.example.com/12-23-34
for e.g. the above written is my page. Now what I need is to get 12, 23, and 34 seperatley. How can I achieve that?
Looked for the solution, but everywhere I got the solution to get 12-23-34 together.
once you get "12-23-34", do an $myArray = explode("-",$myString), where $myArray will have 3 item : 12, 23, and 34...
You first need to get the current URI.
$uri = $_SERVER['REQUEST_URI'];
This will get you /12-23-34.
Ignore the / by getting a substring.
$sub = substr($uri, 1);
Explode the substring into parts.
$parts = explode('-', $sub);
Loop through the parts to do what you want.
foreach ($parts as $part) {
}
Here is solution for you:
$subURL = explode('/', $url); // separates url into www.example.com and 12-23-34
$numbers = explode('-', $subURL[1]); // separates 12, 23 and 34.
echo $numbers[0]; // returns 12
echo $numbers[1]; // returns 23
echo $numbers[2]; // returns 34
As mentioned remove the part you want - the path in your url and then explode it. You need to be sure your url always has a similar pattern.
$url = "www.example.com/12-23-34";
$pos = strrpos($url, "/");
$path = substr($url,$pos+1);
$my_numbers = explode("-",$path);
foreach ($my_numbers as $my_number) {
print "$my_number<br>";
}
Here is what I've used:
<?php
$value = basename(__FILE__, '.php');
echo $value;
$numbers = explode('-', $value);
echo $numbers[0];
echo $numbers[1];
echo $numbers[2];
?>

How to get Implode get data before this string?

My url is
likehttp://localhost/manishatutors/tutors-in-city/Crossing-Republik-tutor/
how could i get Crossing Republic
using php
I used
<?php
list($a,$page_get) = explode("city/",$_SERVER['REQUEST_URI']);
$array=explode("/",$page_get);
$getCity1=remove_dash($array[0]);
$p=$array[1];
$get_city = implode('-',$getCity1);
print_r($get_city);
?>
but its giving
Crossing republik tutor
while I don't want tutor
use explode function and take the last
$req_uris = explode('/',$_SERVER['REQUEST_URI']);
echo $req_uris[count($req_uris)-1];
and if you want you can replace dash with space
echo str_replace('-', ' ', $req_uris[count($req_uris)-1]);
EDIT
$url = 'http://localhost/manishatutors/tutors-in-city/Crossing-Republik-tutor/';
$exploded = array_values(array_filter(explode('/',$url)));
$last = $req_uris[count($exploded)-1];
echo str_replace( '-', ' ', str_replace('tutor', '', $last) );
change $url with $_SERVER['REQUEST_URI']
You may try this
<?php
list($a,$val) = explode("city/",$_SERVER['REQUEST_URI']);
$array=explode("/",$val);
$val2=remove_dash($array[0]);
$p=$array[1];
$val3= implode('-',$val2);
print_r($val3);
?>
NEW EDITED ANSWER
list($a,$val) = explode("city/",$_SERVER['REQUEST_URI']);
$array=explode("/",$val);
$val2=$array[0];
$p=$array[1];
$val3= implode('-tutor',$val2);
print_r(remove_dash($val3[0]));
?>

Get number from an url in PHP

I have the following url : http:example.com/country/France/45.
With the pattern http:example.com/country/name/**NUMBER**(?$_GET possibly).
How can I extract the number with a regex (or something else then regex) ?
With regexp:
$str = 'http:example.com/country/France/45';
preg_match('/http:example\.com\/country\/(?P<name>\w+)\/(?P<id>\d+)/', $str, $matches);
print_r($matches); // return array("name"=>"France", "id" => 45);
$url = 'http:example.com/country/France/45';
$id = end(explode('/',trim($url,'/')));
Simple isn't ?
The usage of trim () is to remove trailing \
echo $last = substr(strrchr($url, "/"), 1 );
strrchr() will give last occurence of the / character and then substr() gives string after it.
use something like this
$url = "http://example.com/country/France/45";
$parts = explode('/', $url);
$number = $parts[count($parts) - 1];
and if you have GET variable at the end, you can explode further like this
$number = explode('?', $number);
$number = $number[0];
hope this helps :)
the get command for php is $_GET so to show to number do
<html>
<body>
<?php
echo $_GET["eg"];
?>
</body>
</html>
with a URL of http:example.com/country/name/?eg=**NUMBER**
Use explode():
$parts = explode('/', $url);
$number = $parts[count($parts)-1];

Extracting part of a string?

How can I extract 4 from this string?
$string = "Rank_1:1:4";
I'm trying to get pagerank from Googles server, and the last value (4) is the actual pagerank.
Try
$string = "Rank_1:1:4";
$data = explode(':',$string);
echo end($data);
EDIT
as per #MichaelHampton, if they add more fields later, then use as below
$string = "Rank_1:1:4";
$data = explode(':',$string);
echo $data[2];
PHP has so many string function you can use ...
Variables
$find = ":";
$string = "Rank_1:1:4";
Using substr
echo substr($string, strrpos($string, $find) + 1);
Using strrchr
echo ltrim(strrchr($string, $find),$find);
$pattern = '/:\d+$/';
preg_match($pattern, $string, $matches);
$rank = substr($matches[0],1);

Categories