Having directory named "Łęć"
and using glob like this:
$dirs = glob( FILES . '/general/*' );
Gives me the result of:
...
(string) "../pliki/general/Logo"
(string) "../pliki/general/���"
(string) "../pliki/general/Maski"
...
And this ��� is the directory named Łęć
I totally can't figure it out how to make it work, so I can have folders with special characters and the glob() to work with it properly
$dirs = glob( FILES . '/general/q/*' );
foreach($dirs as &$dir)
{
$dir = bin2hex($dir);
}
dd($dirs);
This code above globs where Łęć folder is and bin2hex it's name returns: 2e2e2f706c696b692f67656e6572616c2f712fa3eae6 and the folder name alone without the path is a3eae6
a3eae6 is the hexadecimal representation of the string of unknown encoding returned for "Łęć". The string returned by glob() can write in PHP-Notation as "\xa3\xea\xe6". The conversion of this character string with an encoding unknown to us into UTF-8 must then result "Łęć".
Through trial and error, I found that the "ISO-8859-2" encoding satisfies this condition:
$strCode = "\xa3\xea\xe6";
$name = mb_convert_encoding($strCode,"UTF-8","ISO-8859-2");
var_dump($name === "Łęć"); //bool(true)
The strings that glob returns must all be converted with mb_convert_encoding:
$fullNameUTF8 = mb_convert_encoding($strFromGlob,"UTF-8","ISO-8859-2");
This procedure is not certain. It's better to know the exact encoding used by the file system you are accessing.
Related
I am going to change my filename's encode type from utf-8 to big5, and this is what I have so far:
$path = "stu_resume/104206002_87";
$result =iconv("utf-8", "big5", $path);
echo $result;
echo mb_detect_encoding($result);
Within the folder of 104206002_87, there are 2 files, which are 104206002_87_履歷, 104206002_87_自傳. After the code above is executed, I found that there is nothing changed in the folder. Does anyone know how to solve the problem? Thanks a lot.
iconv() doesn't modify files. It just converts a string. In this case, the string it's converting is ""stu_resume/104206002_87" -- since this string only contains ASCII characters, nothing changes when it's converted from UTF-8 to Big5.
If you want to rename the files in the directory with that name, you will need to do so explicitly, e.g.
$iter = new DirectoryIterator("stu_resume/104206002_87");
foreach ($iter as $file) {
if (!$file->isDot()) {
$old_name = $file->getPathname();
$new_name = iconv("utf-8", "big5", $old_name);
rename($old_name, $new_name);
}
}
I am new to php.
I am getting default image path as \xampp\htdocs\Sample\tmp/one.jpg
and i have to replace \xampp\htdocs with http:\localhost
If i use in this way i am getting required output
$absolute = "\xampp\htdocs";
$relative = "http:\\localhost";
$imagepathurl = "\xampp\htdocs\Sample\tmp\/safety-masks_5092610ad4f3f.jpg";
echo str_replace($absolute,$relative,$imagepathurl);
But i am getting the $imagepathurl from database. If i use this in same formate i am not getting result
$absolute = "\xampp\htdocs";
$relative = "http:\\localhost";
$imagepathurl = '"'.$imagepath.'"';
echo str_replace($absolute,$relative,$imagepathurl);
In the secound listing when you add " before and after the $imagepath why?
Remove this line $imagepathurl = '"'.$imagepath.'"'; and it should work:
$absolute = "\xampp\htdocs";
$relative = "http:\\localhost";
echo str_replace($absolute,$relative,$imagepath);
Update (Example what the line does)
$var = "MyString"; // Content of the String => MyString
$var = '"'.$var.'"'; // Content of the String => "MyString"
Use the function pathinfo to get an array as result with all the components of your path.
Then you can replace the part that you want to change and concatenate the path components again to get the desired new path.
The way commented in the other posts is just a string manipulation solution. If you want something safe when dealing with parsing paths you should use the php function pathinfo. If is some quite simple then the str_replace will do the work.
make sure that your database does not applies any character encoding on string if it applies any then first decode the sting value and then use this code.
$absolute = "C:\xampp\htdocs";
$relative = "http:\\localhost";
// if database contains encoded string decode first here
//creating absolute path for image (this will remove ./, ../ \, / to create operating system preferred path as windows uses \ and linuz user / as path separator )
$imagepathurl = realpath($imagepathurl);
// replasing absolute path to root directory with relative path
echo str_replace( $absolute , $relative , $imagepathurl);
I have a string with a path like so:
C:/myfolder/mysubfolder/myfile.doc
I need to truncate the string to become:
myfile.doc
Needless to say, I have a list of such paths with different lengths and different folder depths. What I need is something like trancating the rest of the string beginning from the last of the string till the first / is encountered.
How can I achieve this in PHP.
Many thanks
$path = 'C:/myfolder/mysubfolder/myfile.doc':
$filename = basename($path);
echo $filename; // "myfile.doc"
See Manual "basename()". For more detailed information about a path see pathinfo()
You can use the basename() function for this.
echo basename("C:/myfolder/mysubfolder/myfile.doc");
Output:
myfile.doc
Note that this doesn't exactly do what you described, because for these inputs:
/etc/
.
it would return
etc
.
So an (untested) alternative could be:
$fullname = "C:/myfolder/mysubfolder/myfile.doc";
$parts = explode("/", $fullname);
$filename = $parts[count($parts)-1];
echo $filename;
If you mean you want the filename segment, you can use basename()
http://php.net/manual/en/function.basename.php
My script lists out files in the directory. I am able to use preg_match and regex to find files whose filenames contain integers.
However, this is what I am unable to do: I want an entire string to be omitted if it contains an integer.
Despite trying several methods, I am only able to replace the integer itself and not the entire line. Any help would be appreciated.
if (preg_match('/\d/', $string))
$string = "";
This will turn a string into an empty one if it has any number in it.
According to your description, this should be sth. like:
$files = array();
$dirname = 'C://Temp';
$dh = opendir($dirname) or die();
while( ($fn=readdir($dh)) !== false )
if( !preg_match('/\d+|^\.\.?$/', $fn) )
$files[] = $fn;
closedir($dh);
var_dump($files);
... which reads all file names and stores them (except these with numbers and ../.) in an array '$files', which itself gets displayed at the end of the snipped above. If that doesn't fit your requirement, you should give a more detailed explanation of what you are trying to do
Regards
rbo
I have a following string and I want to extract image123.jpg.
..here_can_be_any_length "and_here_any_length/image123.jpg" and_here_also_any_length
image123 can be any length (newimage123456 etc) and with extension of jpg, jpeg, gif or png.
I assume I need to use preg_match, but I am not really sure and like to know how to code it or if there are any other ways or function I can use.
You can use:
if(preg_match('#".*?\/(.*?)"#',$str,$matches)) {
$filename = $matches[1];
}
Alternatively you can extract the entire path between the double quotes using preg_match and then extract the filename from the path using the function basename:
if(preg_match('#"(.*?)"#',$str,$matches)) {
$path = $matches[1]; // extract the entire path.
$filename = basename ($path); // extract file name from path.
}
What about something like this :
$str = '..here_can_be_any_length "and_here_any_length/image123.jpg" and_here_also_any_length';
$m = array();
if (preg_match('#".*?/([^\.]+\.(jpg|jpeg|gif|png))"#', $str, $m)) {
var_dump($m[1]);
}
Which, here, will give you :
string(12) "image123.jpg"
I suppose the pattern could be a bit simpler -- you could not check the extension, for instance, and accept any kind of file ; but not sure it would suit your needs.
Basically, here, the pattern :
starts with a "
takes any number of characters until a / : .*?/
then takes any number of characters that are not a . : [^\.]+
then checks for a dot : \.
then comes the extension -- one of those you decided to allow : (jpg|jpeg|gif|png)
and, finally, the end of pattern, another "
And the whole portion of the pattern that corresponds to the filename is surrounded by (), so it's captured -- returned in $m
$string = '..here_can_be_any_length "and_here_any_length/image123.jpg" and_here_also_any_length';
$data = explode('"',$string);
$basename = basename($data[1]);