I want to remove a special character from a string in my php page, for that I use str_replace() function. But it does't work for my script. The string is getting from server. I am using the following php code to replace that string.
$path= "catalog\/demo\/samsung_tab_1.jpg";
$newPath = str_replace("\/","/",$path);
But the above str_replace() function is not working properly in my script.
I want to get the output like,
catalog/demo/samsung_tab_1.jpg
Please help.
Instead of \/ you can remove forward slash by using double backslashes:
<?php
$path= "catalog\/demo\/samsung_tab_1.jpg";
$newPath = str_replace("\\","",$path); // replace with empty string ""
echo $newPath; // catalog/demo/samsung_tab_1.jpg
?>
<?php
$path= "catalog\/demo\/samsung_tab_1.jpg";
if (preg_match('/\//', $path)){
echo $newPath = str_replace("\/","/",$path);
}else{
echo $newPath = $path;
}
?>
I hope this will work for you.
Related
How do I add a string after the last slash in url?
current url: https://example.org/gallery/images/my-image.jpg
I need to add the "thumbs/" character to the last slash
The function or php code must change the address as follows
https://example.org/gallery/images/thumbs/my-image.jpg
please guide me
There are a lot of ways. Try using URL and path functions and replacing:
$string = str_replace($dir=dirname(parse_url($string, PHP_URL_PATH)),
"$dir/thumbs",
$string);
Or string functions:
$string = str_replace($s=strrchr($string, '/'), "/thumbs$s", $string);
Concatenate the strings.
<?php
$addString = "thumbs/";
$newURL = "https://example.org/gallery/images/" . $addString . "my-image.jpg";
echo $newURL;
Hi i want to know how can i get substring from string after last slash?
In short i want to get the file name from path.
for example i got string like this:
test-e2e4/test-e2e4/test-e2e4/6.png
and i want to get 6.png how can i do that ?
I got dir only and the file name can be all format, it can be also something else then file
Or test-e2e4/test-e2e4/test-e2e4/aaaaa and want to get aaaaa
Regex maybe? Or maybe you know some nice functions which will do it for me ?
In addition to other replies, there's actually a function in PHP to do this: basename. Example:
$string = 'test-e2e4/test-e2e4/test-e2e4/6.png';
$base = basename($string); // $base == '6.png';
$string = 'test-e2e4/test-e2e4/test-e2e4/aaaaa';
$base = basename($string); // $base == 'aaaaa'
$string = '6.png';
$base = basename($string); // $base == '6.png'
Full details here: http://php.net/basename
Do like this..
$yourstring = 'test-e2e4/test-e2e4/test-e2e4/6.png';
$val = array_pop(explode('/',$yourstring)); // 6.png
You can try explode and array_pop functions to work this out:
$str = 'test-e2e4/test-e2e4/test-e2e4/aaaaa.png';
$str = explode('/', $str);
$filename = array_pop($str);
echo $filename; //Output will be aaaaa.png
...Or you can use the following regex:
[^\/]*$
You don't need to use regex for this, you can use substr() to get a portion of the string, and strrpos() to specify which portion:
$full_path = "test-e2e4/test-e2e4/test-e2e4/6.png"
$file = substr( $full_path, strrpos( $full_path, "/" ) + 1 );
substr() returns a portion of the string, strrpos() tells it to start from the position of the last slash in the string, and the +1 excludes the slash from the return value.
I need a regular expression that would take the string after the last forward slash.
For example, considering I have the following string:
C:/dir/file.txt
I need to take only the file.txt part (string).
Thank you :)
You don't need a regex.
$string = "C:/dir/file.txt";
$filetemp = explode("/",$string);
$file = end($filetemp);
Edited because I remember the latest PHP spitting errors out about chaining these types of functions.
If your strings are always paths you should consider the basename() function.
Example:
$string = 'C:/dir/file.txt';
$file = basename($string);
Otherwise, the other answers are great!
The strrpos() function finds the last occurrence of a string. You can use it to figure out where the file name starts.
$path = 'C:/dir/file.txt';
$pos = strrpos($path, '/');
$file = substr($path, $pos + 1);
echo $file;
I have a problem with getting image from url. I'm using file_put_contents and I found that problem is white spaces in image url because images without any whitespace working.
The URL I'm getting image looks that:
/support/member_profile/16-New%20Image%20(With%20Logo)%20(Medium).jpg
I tried with urlencode() but it's still not working. If I echo encoded url I get:
%2Fsupport%2Fmember_profile%2F16-New+Image+%28With+Logo%29+%28Medium%29.jpg
How can I solve that problem? Thanks in advance
EDIT:
I figured out that I would need to replace ONLY whitespaces with %20. When using urlencode it encode entire URL so that's why it's not working.
Any tip how to do it? Thanks
Use urldecode to convert %20 into real spaces.
Then you can call file_put_contents.
You should only URL-encode the filename, not the entire path including the slashes:
$path = '/support/member_profile/16-New Image (With Logo) (Medium).jpg';
$p = pathinfo($path, PATHINFO_DIRNAME);
$f = pathinfo($path, PATHINFO_FILENAME);
$e = pathinfo($path, PATHINFO_EXTENSION);
echo sprintf('%s/%s.%s', $p, urlencode($f), urlencode($e));
And, actually, you need to urlencode each path-part as well.
$path = '/support/member profile/16-New Image (With Logo) (Medium).jpg';
$p = explode('/', $path);
foreach ($p as $pp)
$pathparts[] = urlencode($pp);
echo implode('/', $pathparts);
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);