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;
Related
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];
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);
Suppose I have the following piece of code:
$myString = 'FilE.EXE';
strlower($myString);
I want to make the name minus its extension to lower case, but the code above will make the entire string into lower case. Is there a way I can just change the name without the extension? If so, what is the most dynamic way to accomplish this?
Desired output: 'file.EXE';
Using pathinfo
$myString = 'FilE.EXE';
$new_string = strtolower(pathinfo($myString, PATHINFO_FILENAME)) . '.' . pathinfo($myString, PATHINFO_EXTENSION);
echo $new_string;
You need to do something like this:
$string = "FilE.EXE";
list($name, $extension) = explode('.', $string);
$string = implode('.', array(strtolower($name), $extension));
Hope it helps.
do:
$myString = 'FilE.EXE';
$txt = strtolower( substr( $myString, 0, strrpos($myString, ".") ) )
.substr( $myString, strrpos($myString, "."), strlen($myString));
echo $txt; //gives file.EXE
You might want to use the pathinfo() function for that:
$myString = 'FilE.iNc.EXE';
$path_parts = pathinfo($myString);
$myNewString = implode('.', array(
strtolower($path_parts['filename']),
$path_parts['extension']
));
So it can ouput this:
file.inc.EXE
<?php
$myString = 'FilE.EXE';
$txt = strtolower( substr( $myString, 0, strrpos($myString, ".") ) );
$hell = substr( $myString, strrpos($myString, "."), strlen($myString));
$babe = $txt.$hell;
echo $babe;
$title = '228-example-of-the-title'
I need to convert the string to:
Example Of The Title
How would I do that?
A one-liner,
$title = '228-example-of-the-title';
ucwords(implode(' ', array_slice(explode('-', $title), 1)));
This splits the string on dashes (explode(token, input)),
minus the first element (array_slice(array, offset))
joins the resulting set back up with spaces (implode(glue, array)),
and finally capitalises each word (thanks salathe).
$title = '228-example-of-the-title'
$start_pos = strpos($title, '-');
$friendly_title = str_replace('-', ' ', substr($title, $start_pos + 1));
You can do this using the following code
$title = '228-example-of-the-title';
$parts = explode('-',$title);
array_shift($parts);
$title = implode(' ',$parts);
functions used: explode implode and array_shift
$pieces = explode("-", $title);
$result = "";
for ($i = 1; $i < count(pieces); $i++) {
$result = $result . ucFirst($pieces[$i]);
}
$toArray = explode("-",$title);
$cleanArray = array_shift($toArray);
$finalString = implode(' ' , $cleanArray);
// echo ucwords($finalStirng);
Use explode() to split the "-" and put the string in an array
$title_array = explode("-",$title);
$new_string = "";
for($i=1; $i<count($title_array); $i++)
{
$new_string .= $title_array[$i]." ";
}
echo $new_string;
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