Let's say I have a windows path name "E:\Dropbox\b\c\d", when I want to change this path to UNIX with some modifications, I can use this python code.
x = "E:\\Dropbox\\b\\c\\d"
print "/Users/prosseek/" + '/'.join(x.split('\\')[1:])
>> /Users/prosseek/Dropbox/b/c/d
What could be the equivalent code in PHP?
Here you go. You could use explode and implode, but there's no real reason to get fancy and use arrays when this is just a basic string operation:
$x = 'E:\Dropbox\b\c\d';
echo '/Users/prosseek/' . str_replace('\\', '/', substr($x, strpos($x, '\\') + 1));
$str = "E:\Dropbox\b\c\d";
$unix = str_replace('E:', '/Users/prosseek', $str);
$unix = str_replace('\\', '/', $unix);
demo
If you're looking for the exact PHP equivalent:
$x = 'E:\Dropbox\b\c\d';
$parts = explode('\\', $x);
array_shift($parts);
print "/Users/prosseek/" . implode('/', $parts);
However, this does not create the output you wanted. Demo.
A pretty much exact equivalent of that Python code would be:
$x = 'E:\\Dropbox\\b\\c\\d';
echo "/Users/prosseek" . implode('/', explode('\\', substr($x, 2)));
Related
I have a string 18-04, I want to make it 04-18.
I have tried doing this :
$myStr = "18-04";
$first = substr($myStr, 0, 1);
$second = substr($myStr, 3, 4);
$final = $second . '-' . $first;
But I was looking for a simpler way rather than writing 5 full lines to do something simple. Any idea?
Use regex in preg_replace() to do this work.
$final = preg_replace("/(\d+)-(\d+)/", "$2-$1", $myStr);
Check result in demo
Is it shorter ? :)
echo implode('-',array_reverse(explode('-',$myStr)));
you can use the following:
$myStr = "18-04";
$chuncks = explode("-",$myStr);
$final = $chuncks[1]. '-' . $chuncks[0];
I have a string "./product_image/Bollywood/1476813695.jpg".
first I remove . from first.
now I want to remove all character between first two / . that means I want
Bollywood/1476813695.jpg
I am trying with this but not work
substr(strstr(ltrim('./product_image/Bollywood/1476813695.jpg', '.'),"/product_image/"), 1);
It always return product_image/Bollywood/1476813695.jpg
Easily done with explode():
$orig = './product_image/Bollywood/1476813695.jpg';
$origArray = explode('/', $orig);
$new = $origArray[2] . '/' . $origArray[3];
result:
Bollywood/1476813695.jpg
If you want something a little different you can use regex with preg_replace()
$pattern = '/\.\/(.*?)\//';
$string = './product_image/Bollywood/1476813695.jpg';
$new = preg_replace($pattern, '', $string);
This returns the same thing and you could, if you wanted, put it all in one line.
$str = "./product_image/Bollywood/1476813695.jpg";
$str_array = explode('/', $str);
$size = count($str_array);
$new_string = $str_array[$size - 2] . '/' . $str_array[$size - 1];
echo $new_string;
please follow the below code
$newstring = "./product_image/Bollywood/1476813695.jpg";
$pos =substr($newstring, strpos($newstring, '/', 2)+1);
var_dump($pos);
and output will be looking
Bollywood/1476813695.jpg
for strpos function detail please go to below link
http://php.net/manual/en/function.strpos.php
for substr position detail please go to below link
http://php.net/manual/en/function.substr.php
How to extract the only name jenir and ellbull from the urls?with out numbers and extension
?
1330001337_jenir.jpg
1330001327_ellbull.png
// output
jenir
ellbull
Use explode on _ or use an regular expression.
I prefer explode for such tasks.
$str = "1330001327_ellbull.png";
list($first,$second) = explode("_", $str);
echo $second;
Now you have ellbull.png.
If you want to loose the .png too, do the same for "." and use first part.
$somevar = pathinfo(preg_replace('/^\d+_/', '', $file_name), PATHINFO_FILENAME);
Will do it as well.
preg_match("/^\\d+_(\\w+)\\.[a-z]+$/", "1330001337_jenir.jpg", $match);
echo ($match[1]) // --> jenir
$var = explode('_', $imgname);
$var = explode('.', $var[1]);
echo $var[0];
without regex.
in your case use
$var = explode('_', $imageName);
echo $var[1];
UPD:
if you do not need file extension,
then you use one more time explode('.', $var[1])
or just echo substr($var[1], -4)
or really use regExp to fetch only the name
If I have a stored input string that looks like this
http://site.com/param1/value1
how can php extract value1?
I know how to extract parameters that look like this
http://site.com?param1=value1
but it doesn't work for the format I'm asking about.
Generally you could parse url with parse_url and then explode path by / , and than read second value in array.
You can use a simple string function combination:
$str = "http://site.com/param1/value1";
$tail = substr($str, strrpos($str, "/") + 1);
Or if it's not sure if there is a / somewhere in the string:
preg_match("#/(\w+)$#", $string, $match);
$tail = $match[1];
For the microoptimizers: this too will generally be faster as the array-explode() workaround.
Fast & Easy:
$url = "http://site.com/param1/value1";
$split_url = explode("/", $url);
$value = $split_url[3];
Looking at some php.net manuals you can easily find this function, that totaly fits your needs
strchr
$url = 'http://example.com/param1/value1';
list($param1, $value1) = array_slice(explode('/', $url), -2, 2);
This will give you param1 and value1 from the example stored in the variables $param1 and $value1.
Look up parse_URL that's the function you want
I would suggest a combination of Trickers and Toby Allens solution
$path = parse_url($url, PHP_URL_PATH);
$segments = explode('/', trim($path, '/'));
$value = $segments[2];
If you have multiple key-value-paris you can ensure with trim(), that the key is always even and the value always odd
$count = count($segments);
$result = array();
for ($i = 0; $i < $count; $i += 2) {
$result[$segments[$i]] = $segments[$i+1];
}
string '/home/adam/Projects/red/storage/22ff0bc0662bd323891844f6ed342cce2603490ec0_tumb_2.jpg' (length=85)
what i need is just
http://localhost/storage/22ff0bc0662bd323891844f6ed342cce2603490ec0_tumb_2.jpg
what is the best way doing it ? i mean useing strlen ? substr_replace ? substr ? im a bit confused what is the best way doing this? becouse there is many ways to do this.
edit* there is no newbie tag :|
// get from database red/storage/22ff0bc0662bd323891844f6ed342cce2603490ec0_tumb_2.jpg
$image_path = $this->data['products'][0]['image_small'];
$exploded = end(explode('/', $image_path));
$myurl = DOMAIN;
$myfullurl = $myurl."/storage/".$exploded;
// it works!, but let see the comments maybe there is a better way :)
Here is how you can get the image part:
$str = '/home/adam/Projects/red/storag/22ff0bc0662bd323891844f6ed342cce2603490ec0_tumb_2.jpg';
$exploded = end(explode('/', $str));
echo $exploded;
Result:
22ff0bc0662bd323891844f6ed342cce2603490ec0_tumb_2.jpg
Now you can concatenate it with whatever eg:
$new_str = 'http://localhost/storage/' . $exploded;
echo $new_str;
Result:
http://localhost/storage/22ff0bc0662bd323891844f6ed342cce2603490ec0_tumb_2.jpg
And It is most likely you want to concatenate the image path with your document root which you do like this:
$img_path = $_SERVER['DOCUMENT_ROOT'] . $exploded;
The idea is that you explode the string with explode function by specifying / as delimiter. This gives you array, now you use the end function to get the ending part of the array which is your image actually.
If the path prefix represents your document root path, then you can do this to strip it:
$path = '/home/adam/Projects/red/storage/22ff0bc0662bd323891844f6ed342cce2603490ec0_tumb_2.jpg';
$_SERVER['DOCUMENT_ROOT'] = '/home/adam/Projects/red/';
if (substr($path, 0, strlen($_SERVER['DOCUMENT_ROOT'])) === $_SERVER['DOCUMENT_ROOT']) {
$uriPath = substr($path, strlen(rtrim($_SERVER['DOCUMENT_ROOT'], '/')));
echo $uriPath;
}
I suggest you check if the string contains /home/adam/Projects/red, and if it does, you use substr to get the part after it, and you glue it with http://localost.
$path = '/home/adam/Projects/red/storage/*snip*.jpg';
$basePath = "/home/adam/Projects/red";
if (strpos($path, $path) !== false)
$url = 'http://localhost' . substr($path, strlen($basePath));
This one's pretty much the easiest
str_replace(
"/home/adam/Projects/red",
"http://localhost",
"/home/adam/Projects/red/storage/22ff0bc0662bd323891844f6ed342cce2603490ec0_tumb_2.jpg"
);
$string = '/home/adam/Projects/red/storage/22ff0bc0662bd323891844f6ed342cce2603490ec0_tumb_2.jpg';
str_replace('/home/adam/Projects/red', 'http://localost', $string)