Asked  7 Months ago    Answers:  5   Viewed   346 times

Possible Duplicate:
how to add Arabic letters to url regex

I have been searching a couple of hours without an answer.

How do you use preg_replace on arabic characters as well as english?

this is my code is english

$string = preg_replace ( "/&([a-zA-Z])(uml|acute|grave|circ|tilde|ring),/", "", $string );
$string = preg_replace ( "/[^a-zA-Z0-9_.-]/", "", $string );

some of the answers suggested i use this code:

$string = preg_replace ( "/&([?-?a-zA-Z])(uml|acute|grave|circ|tilde|ring),/u", "", $string );
$string = preg_replace ( "/[^?-?a-zA-Z0-9_.-]/u", "", $string );

I have tested it and it works. but is this actually functional for php? does it include all arabic characters? is there a better way to include all arabic characters?

What i am going to do with the code is:

Replace all characters in the string to valid SEO friendly characters.

I solved the problem using this code based on Thank you Bryan.

$string = preg_replace ( "/&([x{0600}-x{06FF}a-zA-Z])(uml|acute|grave|circ|tilde|ring),/u", "", $string );
    $string = preg_replace ( "/[^x{0600}-x{06FF}a-zA-Z0-9_.-]/u", "", $string );



The unicode character map is a great place to visualize the groups of characters including Arabic that the first part of the string is grouping for you with [?-?a-zA-Z]

If you are still unsure, read up a little more on regular expressions.

Wednesday, March 31, 2021
answered 7 Months ago

str_replace replaces a specific occurrence of a string, for instance "foo" will only match and replace that: "foo". preg_replace will do regular expression matching, for instance "/f.{2}/" will match and replace "foo", but also "fey", "fir", "fox", "f12", etc.


See for yourself:

$string = "foo fighters";
$str_replace = str_replace('foo','bar',$string);
$preg_replace = preg_replace('/f.{2}/','bar',$string);
echo 'str_replace: ' . $str_replace . ', preg_replace: ' . $preg_replace;

The output is:

str_replace: bar fighters, preg_replace: bar barhters


Wednesday, March 31, 2021
answered 7 Months ago

Try this regular expression in preg_replace


function getPage($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);

    $result = curl_exec($ch);

    if (!preg_match('/src="https?://"/', $result)) {
        $result = preg_replace('/src="(http://([^/]+)/)?([^"]+)"/', "src="\3"", $result);
    if (!preg_match('/href="https?://"/', $result)) {
        $result = preg_replace('/href="(http://([^/]+)/)?([^"]+)"/', "href="\3"", $result);
    return $result;

$result = getPage('');

print_r ($result);
Saturday, May 29, 2021
answered 5 Months ago

try this solution:

get the roboto font as used on android 4 , from here. if that doesn't work , you can try out other fonts , such as the DejaVu font . other fonts are available here for example .

then , put the file into your assets folder , and for each of the views that need to show text (like textView) , use the font :

Typeface face = Typeface.createFromAsset(getAssets(), "Roboto-Regular.ttf");

i've tried hebrew in the past and it showed fine . sadly , the special dots (called "NIKUD") didn't show so well so you might have similar problems for arabic .

Tuesday, August 3, 2021
answered 3 Months ago

Try this:

String number = arabicToDecimal("۴۲"); // number = 42;

private static final String arabic = "u06f0u06f1u06f2u06f3u06f4u06f5u06f6u06f7u06f8u06f9";
private static String arabicToDecimal(String number) {
    char[] chars = new char[number.length()];
    for(int i=0;i<number.length();i++) {
        char ch = number.charAt(i);
        if (ch >= 0x0660 && ch <= 0x0669)
           ch -= 0x0660 - '0';
        else if (ch >= 0x06f0 && ch <= 0x06F9)
           ch -= 0x06f0 - '0';
        chars[i] = ch;
    return new String(chars);
Saturday, August 14, 2021
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :