Abu Rayane
Abu Rayane

Reputation: 241

Replace some characters

I would like to replace some special characters in a text by blank thing:

<?php
$ar = ' بِسْمِ اللَّهِ الرَّحْمَنِ الرَّحِيمِ ';
echo str_replace('/[\u064b-\u0653]/g','', $ar);
echo '<br />';
?>

The sentence is not replace, is there any idea?

Thanks in advance

Upvotes: 0

Views: 68

Answers (1)

Hirnhamster
Hirnhamster

Reputation: 7389

You need to use preg_replace to use regex.

Use the "u" modifier to enable unicode support, there is no /g modifier afaik...

<?php
$ar = ' بِسْمِ اللَّهِ الرَّحْمَنِ الرَّحِيمِ ';
echo preg_replace('/[\\x{064b}-\\x{0653}]/u','', $ar);
echo '<br />';
?>

See http://php.net/manual/en/reference.pcre.pattern.modifiers.php:

u (PCRE_UTF8)

This modifier turns on additional functionality of PCRE that is incompatible with Perl. Pattern and subject strings are treated as UTF-8. This modifier is available from PHP 4.1.0 or greater on Unix and from PHP 4.2.3 on win32. UTF-8 validity of the pattern and the subject is checked since PHP 4.3.5. An invalid subject will cause the preg_* function to match nothing; an invalid pattern will trigger an error of level E_WARNING. Five and six octet UTF-8 sequences are regarded as invalid since PHP 5.3.4 (resp. PCRE 7.3 2007-08-28); formerly those have been regarded as valid UTF-8.

Upvotes: 2

Related Questions