相关疑难解决方法(0)

清理字符串以使其URL和文件名安全吗?

我试图想出一个功能,它可以很好地清理某些字符串,以便它们可以安全地在URL中使用(如post slug),也可以安全地用作文件名.例如,当有人上传文件时,我想确保从名称中删除所有危险字符.

到目前为止,我已经提出了以下函数,我希望能解决这个问题并允许外部UTF-8数据.

/**
 * Convert a string to the file/URL safe "slug" form
 *
 * @param string $string the string to clean
 * @param bool $is_filename TRUE will allow additional filename characters
 * @return string
 */
function sanitize($string = '', $is_filename = FALSE)
{
 // Replace all weird characters with dashes
 $string = preg_replace('/[^\w\-'. ($is_filename ? '~_\.' : ''). ']+/u', '-', $string);

 // Only allow one dash separator at a time (and make string lowercase)
 return mb_strtolower(preg_replace('/--+/u', '-', $string), …
Run Code Online (Sandbox Code Playgroud)

php url filenames sanitization

134
推荐指数
10
解决办法
17万
查看次数

正则表达式清理(PHP)

我想清理URL中的字符串,这是我基本上需要的.

  1. 除字母数字字符和空格以及虚线外,必须删除所有内容.
  2. 空间应该转换成破折号.

例如.

This, is the URL!
Run Code Online (Sandbox Code Playgroud)

必须回来

this-is-the-url
Run Code Online (Sandbox Code Playgroud)

php regex sanitization preg-replace

19
推荐指数
1
解决办法
3万
查看次数

PHP内部编码

根据bin2hex我的PHP在内部使用UTF-8:

echo bin2hex("ö"); -> c3b6 (utf-8)
echo bin2hex(utf8_decode("ö")); -> f6 (ISO-8859)
Run Code Online (Sandbox Code Playgroud)

但mb_internal_encoding()和iconv_get_encoding()都表示它是ISO-8859-1.

echo mb_internal_encoding(); -> ISO-8859-1
var_dump(iconv_get_encoding()); ->  ["input_encoding"]=>  string(10) "ISO-8859-1"  ["output_encoding"]=>  string(10) "ISO-8859-1"  ["internal_encoding"]=>  string(10) "ISO-8859-1"
Run Code Online (Sandbox Code Playgroud)

UTF-8似乎显然是它正在使用的那个,但为什么它仍然显示ISO-8859-1?

php encoding

5
推荐指数
1
解决办法
6395
查看次数

标签 统计

php ×3

sanitization ×2

encoding ×1

filenames ×1

preg-replace ×1

regex ×1

url ×1