title: php : Looking for specific character(s) in a URL Query - struggling with non-ascii
body: <p>Well - the title basically says it.<br>
  I want to look at the URLs query, and look for specific values (singular character, or a small string).  </p>
  
  <p>I can do this successfully - so long as I'm only looking for "normal" characters (those that are often termed as "safe" [a-zA-Z0-9-_.~] ).<br>
  As soon as I start looking for 'unsafe' or 'foreign' characters - it gets ugly.<br>
  I've spent the entire day (and part of yesterday too) attempting to figure this out.<br>
  I've read tons ... RFCs, php.net for encode stuff, detect encode etc. to.<br>
  I've even attempted to set the encode/charset at the top of the script etc. too.<br>
  I've gone through various encode options, setting dynamicsally, manually etc.<br>
  Nothing has worked.</p>
  
  <p>Try the little script below.<br>
  slap it into a file and access it - and append the query path below;<br>
  ?q=a1-.&lt;^ËàÜ  </p>
  
  <h2>See what resutls you get.</h2>
  
  <pre><code>function curPageURL() {
      $pageURL = 'http';
      if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
      $pageURL .= "://";
      if ($_SERVER["SERVER_PORT"] != "80") {
          $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
      } else {
          $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
      }
      return $pageURL;
  }
  
  
  $needles = array(
      needle1 =&gt; 'a', 
      needle2 =&gt; '1', 
      needle3 =&gt; '-', 
      needle4 =&gt; '.', 
      needle5 =&gt; '&lt;',
      needle6 =&gt; '^',
      needle7 =&gt; 'Ë',
      needle8 =&gt; 'à',
      needle9 =&gt; 'Ü'
  );
  
  $haystack = parse_url(curPageURL(), PHP_URL_QUERY);
  
  
  if (!empty($haystack)) {
      $needlelist = implode(' | ',$needles);
  
      echo "We are looking for some needles ( ".$needlelist." ) in a haystack    (".$haystack.")&lt;br/&gt;";
  
      foreach ($needles as $key=&gt;$needle) {
  
          echo "We are looking for ".$key."&lt;br/&gt;";
          $check = strpos($haystack,$needle);
          if ($check !== false) {
              echo " - Yes : we found a needle (".$needle.") in the haystack";
          } else {
              echo " - No : we failed to find the needle (".$needle.") in the haystack";
          }
          echo "&lt;br/&gt;";
  
  }
  
  
  
  echo "--------------&lt;br/&gt;now lets try it with a little basing?&lt;br/&gt;";
  
  
  
  foreach ($needles as $key=&gt;$needle) {
  
      echo "We are looking for ".$key."&lt;br/&gt;";
  
      // Basing - encode the searched for value, and replace any double-encoded % chars
      $needle = str_replace('%25','%',rawurlencode($needle));
  
      $check = strpos($haystack,$needle);
      if ($check !== false) {
          echo " - Yes : we found a needle (".$needle.") in the haystack";
      } else {
          echo " - No : we failed to find the needle (".$needle.") in the haystack";
      }
      echo "&lt;br/&gt;";
  
  }
  }
  </code></pre>
  
  <hr>
  
  <p>I don't know about you, but instead of the strange characters, or their correct hex codes (as per the various lists/tables for urlencoded chars),
  I get the following ([Searched for] (1st results) (2nd results));  </p>
  
  <p>/a a a<br>
    /1 1 1<br>
    /- - -<br>
    /. . .<br>
    /&lt; &lt; %3C<br>
    /^ ^ %5E<br>
    /Ë Ã‹ %C3%8B<br>
    /à Ã  %C3%A0<br>
    /Ü Ãœ %C3%9C  </p>
  
  <p>(<em>/ added to prevent line insertion + the encoding here makes this Very difficult to post!</em>)</p>
  
  <p>the problem is - for example, the last one ...
  Ü should become %DC (as far as I can tell) - so why the paired hex?</p>
  
  <p>I've tried reading up on multibyte stuff ... but I fail to see how the Browsers are encoding the chars in the URL,
  but the script won't.</p>
  
  <p>So - anyone see what I'm doing wrong, or not doing, or figured this out already?</p>
  
  <p>.  </p>
  
  <p>For the sake of Clarity...<br>
  ... I am NOT asking how to replace the characters (I do Not want to turn Ü into U).
  Simply take a given string and see if it is in the URL (straight, or encoded for the URL).</p>
  
  <p>Thanks, and I hope someone can help.</p>