在utf中获得波斯语(波斯语链接)字符

问题描述 投票:0回答:1

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

我有从网站获得Farsi角色的问题:

$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中保存波斯语艺术家名称

名字是:Amir Ali

但是我看到了这个序列:

امیرعلی

在文件中

如何将其保存为波斯语角色?

php utf farsi
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 Link(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');

放在脚本的顶部,可能脚本必须在UTF-8内部编码,没有BOM(字节顺序标记)...

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

我希望这有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.