Get string from URL without slash [duplicate] - php

This question already has answers here:
Extracting the last segment on an URI
(4 answers)
Closed 6 years ago.
I have a URL like http://localhost/m/2016/05/05/sebut-gubernur.
If link is clicked, I want get sebut-gubernur.

The $_SERVER variable helps your. and also explode function.
$part = explode("/", urldecode($_SERVER['REQUEST_URI']));
echo $part[(count($part) - 1)]; //sebut-gubernur
Note: You also need .htaccess code for this kind of url.
OR
If you have the link as a string and want to get sebut-gubernur then try:
$link = 'http://localhost/m/2016/05/05/sebut-gubernur';
$part = explode("/", urldecode($link));
echo $part[(count($part) - 1)]; //sebut-gubernur

echo substr($url, strrpos($url, '/') + 1);

Related

Laravel: Regex to strip part of a url [duplicate]

This question already has answers here:
extract part of a path with filename with php
(2 answers)
Closed 2 years ago.
$string = "https:\/\/s3.us-west-2.amazonaws.com\/dev-lopes\/videos\/585903773a9_654641765275736"
My intention is to have the videos path (stripping the escaped forward slash) and the identifier set to a variable, like videos/585903773a9_654641765275736
Is this possible using str_replace or is this strictly regex required?
Try this Way. I think it will help you. I hope you will get your desire output.
<?php
// Your code here!
$string = "https:\/\/s3.us-west-2.amazonaws.com\/dev-lopes\/videos\/585903773a9_654641765275736";
//remove back slashes
$str1 = stripslashes($string);
//count total string length.
$len = strlen($str1);
//pick the position of /videos
$pos = strpos($str1,'/vid');
//split it from the url.
$break_string = substr($str1, $pos, $len);
//output: /videos/585903773a9_654641765275736
echo($break_string);
?>

use str_replace to remove after # [duplicate]

This question already has answers here:
removing #email.com from string in php
(6 answers)
Closed 4 years ago.
How can I use the str_replace() without using an array of words for turning:
some#mail.com
into
some
So, everything after the '#' sign includes # as well.
How can I do that?
As an example, i will type 'adminofsite#xxxwwweeerandomstuff.com'
and the output will be: 'adminofsite'.
use strstr
$email="john#doe.com"
$user = strstr($email, '#', true);
echo $user;
$str = "some#mail.com"
$str = substr($str,0,strpos($str,"#"))
function stripEmailDomain($string){
return substr($string, 0, strpos($string, '#'));
}
I exploded and then rebuilt the string. This will work if you are positive the string will always be an email address. Its a work around but seems flexible if you want to modify it for a specific purpose.
<?php
$explo0= explode('#',"some#mail.com");
for($i=0;$i<count($explo0);$i++){
$exploresult0.=$explo0[$i+1];
}
echo "#".$exploresult0;
?>

How to get string before last slash and after second last slash in URL [duplicate]

This question already has answers here:
Get Last Part of URL PHP
(14 answers)
Closed 1 year ago.
If I have a URL look like this:
http://wwww.example/test1/test2/test3/
How can I retrieve string test3 from the url above?
$str = explode("/","http://wwww.example/test1/test2/test3/");
echo $str[count($str)-2];
DEMO: https://eval.in/83914
Regular expression solution.
$url = 'http://wwww.example/test1/test2/test3/';
preg_match('#.*/(.*)/$#', $url, $matches);
echo $matches[1];
Demo
Using capturing group, $:
preg_match('!/([^/]+)/[^/]*$!', 'http://wwww.example/test1/test2/test3/', $matches);
echo $matches[1];
DEMO: http://ideone.com/7EVfZa
So basename() does exactly that.
$url = 'http://www.example/test1/test2/test3/';
echo basename($url); // returns : test3;

Getting domain extension from URL? [duplicate]

This question already has answers here:
Extract domain from url (including the hard ones) [duplicate]
(3 answers)
Closed 9 years ago.
I want to get the domain extension from the url. eg. .com, .net etc.
I have used this:
$extension = pathinfo($_SERVER['SERVER_NAME'], PATHINFO_EXTENSION);
I was just wondering if there was a better way using parse_url?
Use parse_url(). Something like:
$host = parse_url('http://www.google.co.uk/test.html');
preg_match('/(.*?)((\.co)?.[a-z]{2,4})$/i', $host['host'], $m);
$ext = isset($m[2]) ? $m[2]: '';
Edit: Fixed with regex from this answer. Extension like .co.uk are supported too.
You can easily split the requested URI like this:
function tld( $uri ) {
$parts = explode('.', $uri);
return (sizeof($parts) ? ('.' . end($parts)) : false;
}
So you can do this:
if(!tld('googlecom')) // does not contain an ending TLD
if(tld('google.com')) // this is a valid domain name; output: ".com"
Or you can just use:
echo $_SERVER['SERVER_NAME'];
So in my oppinion the most best example of usage is:
echo tld($_SERVER['SERVER_NAME']);

regex URL cut question mark [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Parsing Domain From URL In PHP
How do I parse a URL in PHP?
I have this code:
$url = preg_replace("/^(http:\/\/)*(www.)*/is", "", $url);
$url = preg_replace("/\/.*$/is" , "" ,$url);
It work good get domain name where after .com is / . I got problem if there is url like this:
http://www.something.com?id=21213
What do I need to add to regex to cut ?id=21213 so there remains only something.com
See the magical build in function of parse_url().
Using parse_url(), you can retrieve the domain name solely by:
$domain = parse_url($url, PHP_URL_HOST);
if(0 === strpos('www.', $domain)){
$domain = substr($domain, 4);
}

Categories