Skip to content

Instantly share code, notes, and snippets.

@hamidreza-s
Created January 15, 2013 20:15
Show Gist options
  • Save hamidreza-s/4541622 to your computer and use it in GitHub Desktop.
Save hamidreza-s/4541622 to your computer and use it in GitHub Desktop.
List directory file/folder with PHP and create an array of them, then calculate their Character Code.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="application/xhtml+xml; charset=UTF-8" />
</head>
<body>
<?php
// shell command
$list = shell_exec('ls');
// split by newline
$listArray = preg_split('/\n|\r\n?/', $list);
// remove last index
array_pop($listArray);
// echo
foreach ($listArray as $item)
{
echo 'File/Folder Name: ' . $item . '<br/>';
echo 'Charset: ' . mb_detect_encoding($item) . '<br/>';
echo 'Ascii: ' . string_to_ascii($item) . '<hr/>';
}
// convert string to sum of its characters
function string_to_ascii($string)
{
$ascii = NULL;
for ($i = 0; $i < strlen($string); $i++)
{
$ascii .= ord($string[$i]) . ' ';
}
return $ascii;
}
?>
</body>
</html>
@sameei
Copy link

sameei commented Mar 10, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment