Hello everyone i have a pattern that checks the given url is correct or not and this is working perfectly fine, but when i give arabic chrarcters in pattern it return false because of invalid URL. I want english and arabic chracter to check in URL. where i can do change or what i can add in this pattern so this pattern can also validate arabic character with english

// check if URL is valid format
 $pattern = '/^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w]([-\d\w]{0,253}[\d\w])?\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.,\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.,\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.,\/\d\w]|%[a-fA-f\d]{2,2})*)?$/'; 
 $isLink = preg_match($pattern, $url); // Returns true if a link

Dani AI

Generated

You are running into the URI vs IRI issue. Classic URL regexes (and PHP’s FILTER_VALIDATE_URL) were written for ASCII-only URIs (RFC 3986), so any Arabic letters in the host or path will fail. The standards-based way is: (1) convert the host to ASCII with IDNA/Punycode, and (2) percent-encode any non-ASCII characters in the path/query/fragment. After that, validate the ASCII URL. This mirrors the IRI-to-URI mapping in the spec and works for Arabic as well as English. See RFC 3987 (IRIs) and RFC 3492 (Punycode).

Here is a minimal, drop-in validator that accepts Arabic in both the domain and path, then validates the ASCII form. It requires the intl extension for IDN functions:

function validateIri(string $url): bool {
    $p = parse_url($url);
    if ($p === false || empty($p['scheme']) || empty($p['host'])) return false;

    // 1) Host: Unicode -> ASCII (Punycode)
    if (!function_exists('idn_to_ascii')) return false; // need ext/intl
    $host = idn_to_ascii($p['host'], IDNA_DEFAULT, INTL_IDNA_VARIANT_UTS46);
    if ($host === false) return false;

    // 2) Path/query/fragment: percent-encode UTF-8 safely
    $path = isset($p['path']) ? implode('/', array_map('rawurlencode', explode('/', $p['path']))) : '';
    $query = '';
    if (isset($p['query'])) {
        parse_str($p['query'], $q);
        $query = '?'.http_build_query($q, '', '&', PHP_QUERY_RFC3986);
    }
    $frag = isset($p['fragment']) ? '#'.rawurlencode($p['fragment']) : '';
    $port = isset($p['port']) ? ':'.$p['port'] : '';

    $asciiUrl = $p['scheme'].'://'.$host.$port.$path.$query.$frag;
    return filter_var($asciiUrl, FILTER_VALIDATE_URL) !== false;
}

Notes:

  • @diafol’s Unicode property idea is spot-on for showing Arabic letters are present, but for full URL validation you still need the IRI-to-URI steps above. If you insist on regex, add the u modifier and replace \w with Unicode categories like \p{L}\p{N}_, but you must still punycode the host. See idn_to_ascii, filter_var, and rawurlencode.

Recommended Answers

All 4 Replies

No ideas.......

Member Avatar for Member #120589
$pattern = "/[^\p{Arabic}]/u";
$text = "كتابxz";

preg_match_all($pattern,$text,$matches);
print_r($matches);

Will show 'x' and 'z' as not Arabic

When you look for the positive...

$pattern = "/\p{Arabic}/u";
$text = "كتابxz";

preg_match_all($pattern,$text,$matches);
echo "<pre>";
print_r($matches);
echo "</pre>";

I get this:

Array
(
    [0] => Array
        (
            [0] => ك
            [1] => ت
            [2] => ا
            [3] => ب
        )
)

Is that a start for you?

Hi Diafol
Thanks for your reply

So can i add this /\p{Arabic}/u in my exiting pattern that validate URL?

Member Avatar for Member #120589

I think that arabic is urlencoded, so you may be able to get the original arabic back via urldecode. As I've never done this myself, I'm only speculating.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.