I have URL like
https://example.com/something/this-is-my-part/10448887
and want to have
this-is-my-part
only.
Is there an option for this?
Something like this should work:
$path = parse_url($url, PHP_URL_PATH);
$parts = explode('/', $path);
$part = $parts[count($parts) - 2];
Sure:
$url = "https://example.com/something/this-is-my-part/10448887";
$path = parse_url($url, PHP_URL_PATH);
$segments = explode("/", $path);
echo $segments[2];
// Remove final slash if it exists
$string = rtrim($string, '/');
// Explode the string into an array
$parts = explode('/', $string);
// Echo 2nd to last item
echo $parts[count($parts) - 2];
Related
I have string like :
/home/kamal/public_html/clients/book/wp-content/uploads/wpcf7_uploads/1983322598/k.jpg
i need to get only
wp-content/uploads/wpcf7_uploads/1983322598/k.jpg
How can do that ?
$string = "/home/kamal/public_html/clients/book/wp-content/uploads/wpcf7_uploads/1983322598/k.jpg";
$exploded_string = explode("/", $string);
//$exploded_string is now an array of: home, kamal, public_html.... ...k.jpg
$n = array_search("wp-content", $exploded_string);
//$n is now the index of "wp-content" in the $exploded_string array
$new_path = array_slice($exploded_string, $n);
$new_path = implode("/", $new_path);
echo $new_path;
I have URLs like this:
http://www.mywebsite.com/carmake/ABCDEFG/123456789
http://www.mywebsite.com/carmake/AAABBBC/124532532
http://www.mywebsite.com/carmake/BNDFKNV/463634213
and I want to change them to this:
http://www.mywebsite.com/carmake/parts/123456789
http://www.mywebsite.com/carmake/parts/124532532
http://www.mywebsite.com/carmake/parts/463634213
How can I change the text between the last to slashes to parts in functions.php
https://regex101.com/r/sH1wA1/1
<?php
$string = 'http://www.mywebsite.com/carmake/ABCDEFG/123456789';
$pattern = '/(.*\/).*\/([^\/]*$)/';
$replacement = '${1}parts/${2}';
echo preg_replace($pattern, $replacement, $string);
?>
Try this:
<?php
$url = "http://www.mywebsite.com/carmake/ABCDEFG/123456789";
$parts = parse_url($url);
$path = $parts['path'];
$pos = strpos($path, '/', 9);
$sub = substr($path, 9, $pos - 9);
$url = str_replace($sub, 'parts', $url);
Split to segments, change and collect back
$a = 'http://www.mywebsite.com/carmake/BNDFKNV/463634213';
$to = 'parts';
$s = explode('/', $a);
$s[count($s)-2] = $to;
echo implode('/', $s);
http://www.mywebsite/product-tag/animal/
How to I echo animal without the backslash and in big caps like this:
ANIMAL
$link = 'http://www.mywebsite/product-tag/animal/';
$parts = explode( '/', $link );
echo(strtoupper($parts[4]));
If you want auto searching, then you need to use preg_match.
<?php
$string = 'http://www.mywebsite/product-tag/animal/';
$urlparts = explode("/", $string);
$animal = ($string[strlen($string)-1] == '/'? $urlparts[count($urlparts)-2] : end($urlparts));
echo strtoupper($animal);
?>
actually I just figured it out
$r = $_SERVER['REQUEST_URI'];
$r = explode('/', $r);
$r = array_filter($r);
$r = array_merge($r, array());
$endofurl = $r[1];
echo $endofurl;
$hostname = "abc.domain.com"
I just want "abc" and nothing after it.
With substr and strpos:
$host = substr($hostname, 0, strpos($hostname, '.'));
or maybe better, strstr:
$host = strstr($hostname, '.', true);
There are a lot of functions available to process strings.
Use explode():
$parts = explode('.', $hostname);
// $parts[0]
Will it always have a subdomain?
If so, you can just do
$parts = explode('.', $hostname);
$subdomain = $parts[0];
If there might not be a subdomain
$parts = explode('.', $hostname);
$subdomain = count($parts) == 3 ? $parts[0] : NULL;
If I have a string in the following format: location-cityName.xml how do I extract only the cityName, i.e. a word between - (dash) and . (period)?
Try this:
$pieces = explode('.', $filename);
$morePieces = explode('-', $pieces[0]);
$cityname = $morePieces[1];
Combine strpos() and substr().
$filename = "location-cityName.xml";
$dash = strpos($filename, '-') + 1;
$dot = strpos($filename, '.');
echo substr($filename, $dash, ($dot - $dash));
There are a few ways... this one is probably not as efficient as the strpos and substr combo mentioned above, but its fun:
$string = "location-cityName.xml";
list($location, $remainder) = explode("-", $string);
list($cityName, $extension) = explode(".", $remainder);
As i said... there are lots of string manipulation methods in php and you could do it many other ways.
Here's another way to grab the location as well, if you want:
$filename = "location-cityName.xml";
$cityName = preg_replace('/(.*)-(.*)\.xml/', '$2', $filename);
$location = preg_replace('/(.*)-(.*)\.xml/', '$1', $filename);
Here is a regular-expression–based approach:
<?php
$text = "location-cityName.xml";
if (preg_match("/^[^-]*-([^.]+)\.xml$/", $text, $matches)) {
echo "matched: {$matches[1]}\n";
}
?>
This will print out:
matched: cityName