0

我是 php 的新手,我有一个脚本可以从一些波斯音乐网站获取信息。

我从网站获取波斯语字符有问题:

$this->fa_artist = $html->find('div.main-post', 0)->find('p', 0)->find('b', 1)->plaintext;
file_put_contents('fa_artist.txt', $this->fa_artist);

通过 html 链接在 fa_artist 中保存波斯艺术家姓名

名称是:امیر علی

但我看到了这个序列:

امیرعلی

在文件中

如何将其保存为波斯语字符?

4

1 回答 1

0

UTF-8(unicode)链接应使用 rawurlencode 编码,以标准合规模式提供必要的字符序列......例如:

<?php
  echo '<a href="' . rawurlencode("امیر علی") . '">' . htmlentities("امیر علی", ENT_QUOTES, "UTF-8") . '</a>';
?>

如果你看到源,你可以看到:

<a href="%D8%A7%D9%85%DB%8C%D8%B1%20%D8%B9%D9%84%DB%8C">امیر علی</a>

rawurlencode必须用于 UTF-8 链接 ( http://php.net/manual/en/function.rawurlencode.php )

htmlentities必须用于 UTF-8 文本 ( http://php.net/manual/en/function.htmlentities.php )

您的页面必须使用以下方法以 UTF-8 格式提供:

ini_set('default_charset', 'UTF-8');

放在脚本的顶部,并且可能脚本必须在没有 BOM(字节顺序标记)的情况下以 UTF-8 进行内部编码......

所以你可以在你的项目中直接使用 UTF-8 而不会丢失任何东西......

我希望这会有所帮助。

于 2019-03-11T15:43:36.520 回答