I am getting directory path in my PHP like below
C:\Users\MAITRII\Desktop\LATEST\a
using code like below
$path = getcwd();
Now I want replace "\" with "/" and want remove C: from string and want output like below
/Users/MAITRII/Desktop/LATEST/a
Let me know if someone can help me for achieve this.
As per my comment Use str_replace function but in backslash case use this pattern '\\', '/'
<?php
$path="C:\Users\MAITRII\Desktop\LATEST\a";
$path = str_replace('\\', '/', $path);
print_r($path);
here is the output
and finally you can remove C: using substr function
$path = substr($path, 2);
please try
$path = 'C:\Users\MAITRII\Desktop\LATEST\a';
$path1 = substr($path, 2);
$path2 = str_replace("\\","/",$path1);
echo $path2;
subsrt will remove first two letters i.e. C:
str_replace will replace \ with /
You will need to add additional \ in str_replace("\\","/",$path1);
OUTPUT is here
Related
I have this string
../../some/folder/image.png
and it's possible that the string will be
../../../../../../some/folder/image.png
and I want to remove all ../ and add /root/folder/ in front of some/folder/image.png.
How do I do this?
edit
And sometime that string is placed in the middle. I mean it might be like this:
hallo hallo ../../../some/folder/image.png.
You could do the following :
$path = "hello lol ../../some/folder/image.png";
$path = preg_replace("#.*\.\./#", "", $path);
$path = "/root/folder/" . $path;
Depends where you want to use it, but you would do something like this
$path = "../../../../../some/folder/image.png";
$searchFor = substr($path, 0, strpos( $path, "some"));
$path = str_replace($searchFor, "/root/folder/", $path);
I know the preg_replace() is better, but I wanted to share a different answer
Goal -
convert a path: /aaaaa/bbbbb/ccccc/dddd
to a relative path (to the root): ../../../../
So far I've come up with this regex: /\/.+?\//
but this only produces: ..bbbbb..dddd because it is only matching every other pair of slashes, and also matching the slashes. I'm looking for something like a string split, but also replace.
All of my php code:
$pattern = '/\/.+?\//';
$path = '/aaaaa/bbbbb/ccccc/dddd';
echo preg_replace($pattern, '..', $path);
preg_replace('/\/{0,1}(\w+)\/{0,1}/', '../', $path);
This is working for me.
How about:
preg_replace(':/[^/]+:', '../', $path);
what about the below ?
$pattern = '/\//';
$path = '/aaaaa/bbbbb/ccccc/dddd';
preg_replace(array($pattern), array('..'), $path
I have a path like:
$path='somefolder/foo/bar/lastdir';
and I want to remove the last part, so I have:
$path='somefolder/foo/bar';
Like I went one folder up.
I'm really newbie in php, maybe its just one function, although I can't find it anywhere.
You could try this (tested and works as expected):
$path = 'somefolder/foo/haha/lastone';
$parts = explode('/', $path);
array_pop($parts);
$newpath = implode('/', $parts);
$newpath would now contain somefolder/foo/haha.
use :
dirname(dirname('somefolder/foo/haha/lastone/somescript.php'));
this should return:
somefolder/foo/haha/
This is untested, but try:
$path_array = explode('/',$path);
array_pop($path_array);
$path = implode('/',$path_array);
If you are currently at:
somefolder/foo/haha/lastone/somescript.php
and you want to access:
somefolder/foo/haha/someotherscript.php
just type:
../someotherscript.php
Probably using a regex function would be appropriate if the last part is going to vary. Try
$pattern = '#/.*$#U';
$stripped_path = preg_replace($pattern, '', $original_path);
This will strip everything off the original path string starting from the last forward slash.
You could use a function that explodes() the $path variable into an array and then array_pop to get rid of the last element.
function path($path) {
$arrayPath = explode("/", $path);
$path = array_pop($arrayPath);
return $path = implode("/", $path);
}
The shortest variant in PHP is:
$path = preg_replace('|/[^/]*$|','', $path);
which uses a regular expression.
I'm trying to replace / and \ with //:
$path = 'C:\wamp\www\mysite/bla/bla';
str_replace(array("\/", "\\"), array("\/\/", "\/\/"), $path);
but it doesn't work:(
I get C:\\/wamp\\/www\mysite/bla/bla ...
It is not necessary to escape the forward slash, so it is interfering with the pattern match.
Also, str_replace returns the replacement, it is not a byRef function so you'll need to store the return in a variable (docs).
See it happen: http://codepad.org/CNr8P79m
<?php
$path = 'C:\wamp\www\mysite/bla/bla';
$path = str_replace(array("/", "\\"), array("//", "//"), $path);
echo $path;
// output: C://wamp//www//mysite//bla//bla
?>
You don't need to escape / and you need to assign the return value of str_replace to a variable:
$path = str_replace(array("/", "\\"), array("//", "//"), $path);
If you're trying to normalize paths then I can recommend replacing all directory separators by / as this doesn't interfere with escaping and works on both Linux and Windows.
You don't need to escape slashes only backslashes.
$path = 'C:\wamp\www\mysite/bla/bla';
$path = str_replace(array('/', '\\'), array('//', '//'), $path);
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)