How to pass Arabic string via url (GET method) properly to find substring using php?

399 Views Asked by At

I am trying to pass Arabic search term via url (get method ) like this(user clicks thumbnail image):

<script>
function myClick2(clicked_id) {

window.location = clicked_id;
}


</script>
<div class ="image">
    <a href="javascript:myClick2('./test.php?SearchWord=خوخ')">
        <img src="http://awebsite.com/Thumb0.jpg" alt=".." />
        <figcaption><a href="javascript:myClick2('./test.php?SearchWord=خوخ')">خوخ </a> <br /></figcaption>
    </a>
</div>

The call looks like this after user clicks thumbnail on main page:

http://mywebsite.com/test.php?SearchWord=خوخ 

but i get no output from the script and all i get is :string(0) "" Could any one tell me why my script doesn't output the set of data that starts with search word خوخ ?

<?php

$str = <<<'STR'
&#1578;&#1601;&#1575;&#1581;&#1577;:
<br><img src="http://asite.com/1.jpg"><br>
<a href="https://asite.com/1.html">&#1578;&#1601;&#1575;&#1581;&#1577;</a> <br />

<a href="http://asite.com/link.html">link1 </a> <br />
<a href="http://asite.com/link.html">link2 </a> <br />
<a href="http://asite.com/link.html">link3 </a> <br />
<a href="http://asite.com/link.html">link4 </a> <br />
<a href="http://asite.com/link.html">link5 </a> <br />

--------------------------------------<br>
Mango:
<br><img src="http://asite.com/1.jpg"><br>
<a href="https://asite.com/Mango.html">Mango</a> <br />

<a href="http://asite.com/linkMango.html">link1Mango </a> <br />
<a href="http://asite.com/linkMango.html">link2Mango </a> <br />
<a href="http://asite.com/linkMango.html">link3Mango </a> <br />
<a href="http://asite.com/linkMango.html">link4Mango </a> <br />
<a href="http://asite.com/linkMango.html">link5Mango </a> <br />

--------------------------------------<br>
&#1582;&#1608;&#1582;:
<br><img src="http://asite.com/1.jpg"><br>
<a href="https://asite.com/1.html">&#1582;&#1608;&#1582;</a> <br />

<a href="http://asite.com/linkpeach.html">link1&#1582;&#1608;&#1582; </a> <br />
<a href="http://asite.com/linkpeach.html">link2&#1582;&#1608;&#1582; </a> <br />
<a href="http://asite.com/linkpeach.html">link3&#1582;&#1608;&#1582; </a> <br />
<a href="http://asite.com/linkpeach.html">link4&#1582;&#1608;&#1582; </a> <br />
<a href="http://asite.com/linkpeach.html">link5&#1582;&#1608;&#1582; </a> <br />

--------------------------------------<br>
STR;


$start = $_GET["SearchWord"];
$end = '--------------------------------------<br>';

function get_string_between($string, $start, $end) {
   // make sure we escape all parts of the pattern
    $start = preg_quote($start, '/');
    $end= preg_quote($end, '/');

    // create the pattern
    $pattern = "/$start(.*?)$end/su"; // using s and u pattern modifiers

    if (preg_match($pattern, $string, $match)) {
        return $match[1];
    }
}
echo "<pre>";
var_dump(htmlspecialchars(get_string_between($str, $start, $end)));
echo "<pre>";

?>
1

There are 1 best solutions below

1
On

PHP has a function called iconv

You will want to encode any Arabic to ISO-8859-6 using iconv.

Encode it before passing it into a URL (when they click search) and then decode it within your controller/form function.