我有阿拉伯语文本,并希望在URL中发送它,所以我使用以下函数
$string = 'الغديات والعشيات" لن تنفع!';
$text = preg_replace('/[^p{Arabic}da-z-]/ui', '', $string);
输出:الغدياتوالعشياتلنتنفع
这个函数删除单词之间的空格,这是不期望的。
那么如何避免删除空格呢?还有其他的功能可以帮助我吗?
我使用了"rawurlencode",它对我来说工作得很好。
$text = 'الغديات والعشيات" لن تنفع!';
<a href="http://example.com?text=<?php echo htmlspecialchars(rawurlencode($text)) ?>" >Go</a>
不需要做任何其他事情。谢谢大家的努力。
您不需要正则表达式。它是这样工作的:
<?php
echo '<a href="someplace?ss=', urlencode($string), '">';
?>
urlencode()
自动保留空格为%20
try this:
$string = 'الغديات والعشيات" لن تنفع!';
$string = str_replace(' ', 'ضضض', $string); // save the space
$string = str_replace('!', 'ضضض', $string); // save the !
$string = preg_replace('/[^p{Arabic}da-z-]/ui', '', $string);//remove all none arabic char
$string = str_replace('ضضض', ' ', $string); // return space and "!"
echo $string;