删除 HTML 特殊字符

发布于 2024-09-29 06:52:32 字数 120 浏览 2 评论 0原文

我如何从PHP字符串中删除°之类的特殊字符

实际上我需要从文本文件中读取内容并需要从中删除除字母和数字之外的html特殊字符

How do i remove , , ° like special characters from a string in PHP

Actually i need to read the contents from a text file and need to remove the html special characters except the alphabets and digits from it

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

述情 2024-10-06 06:52:32

或者,如果您想使用正则表达式,请使用:

$str = 'a“bc”def°g';
$str = preg_replace("[^A-Za-z0-9 ]", "", $str);

Or, if you want to use a RegEx, use:

$str = 'a“bc”def°g';
$str = preg_replace("[^A-Za-z0-9 ]", "", $str);
稍尽春風 2024-10-06 06:52:32

一种方法是(例如)

$str = 'a“bc”def°g';
$special = array('“','”','°');
$str = str_replace($special,'',$str);

One way would be (eg)

$str = 'a“bc”def°g';
$special = array('“','”','°');
$str = str_replace($special,'',$str);
情域 2024-10-06 06:52:32

根据编辑 - 要读取文件内容,删除除字母数字和空格字符之外的所有内容,然后将结果写入同一文件,您可以使用:

$file= "yourfile.txt";
$file_content = file_get_contents($file);
$fh = fopen($file, 'w');
$file_content=preg_replace("[^A-Za-z0-9 ]", "", $file_content);
fwrite($fh, $file_content);
fclose($fh);

Per the edit- to read the file contents, remove everything except alphanumerics and space characters then write the result to the same file, you can use:

$file= "yourfile.txt";
$file_content = file_get_contents($file);
$fh = fopen($file, 'w');
$file_content=preg_replace("[^A-Za-z0-9 ]", "", $file_content);
fwrite($fh, $file_content);
fclose($fh);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文