php url to absolute function现在可以使用空格和阿拉伯语字符

时间:2012-03-24 08:03:51

标签: php

我有这些函数url_to_absolute它的工作正常但如果图像网址有空格或阿拉伯字符则不起作用

此工作示例

<?php
    require_once('url_to_absolute.php');
    $url = 'http://site.com/news/view.aspx?id=23032012';
    echo url_to_absolute($url, '/uploadedimages/Sections/Politics/Arabic_World/original/Administrative-detention.jpg'), "\n";
    ?>
look here if we have space in "Arabic World"
<?php
    require_once('url_to_absolute.php');
    $url = 'http://site.com/news/view.aspx?id=23032012';
    echo url_to_absolute($url, '/uploadedimages/Sections/Politics/Arabic World/original/Administrative-detention.jpg'), "\n";
?>

它不起作用,如果还有阿拉伯语字符

1 个答案:

答案 0 :(得分:0)

代码:

<?php
function code2utf($num){
  if($num<128) 
    return chr($num);
  if($num<1024) 
    return chr(($num>>6)+192).chr(($num&63)+128);
  if($num<32768) 
    return chr(($num>>12)+224).chr((($num>>6)&63)+128)
          .chr(($num&63)+128);
  if($num<2097152) 
    return chr(($num>>18)+240).chr((($num>>12)&63)+128)
          .chr((($num>>6)&63)+128).chr(($num&63)+128);
  return '';
}

function unescape($strIn, $iconv_to = 'UTF-8') {
  $strOut = '';
  $iPos = 0;
  $len = strlen ($strIn);
  while ($iPos < $len) {
    $charAt = substr ($strIn, $iPos, 1);
    if ($charAt == '%') {
      $iPos++;
      $charAt = substr ($strIn, $iPos, 1);
      if ($charAt == 'u') {
        // Unicode character
        $iPos++;
        $unicodeHexVal = substr ($strIn, $iPos, 4);
        $unicode = hexdec ($unicodeHexVal);
        $strOut .= code2utf($unicode);
        $iPos += 4;
      }
      else {
        // Escaped ascii character
        $hexVal = substr ($strIn, $iPos, 2);
        if (hexdec($hexVal) > 127) {
          // Convert to Unicode 
          $strOut .= code2utf(hexdec ($hexVal));
        }
        else {
          $strOut .= chr (hexdec ($hexVal));
        }
        $iPos += 2;
      }
    }
    else {
      $strOut .= $charAt;
      $iPos++;
    }
  }
  if ($iconv_to != "UTF-8") {
    $strOut = iconv("UTF-8", $iconv_to, $strOut);
  }   
  return $strOut;
}
?>

尝试:

<?php
   $newUrl = unescape($url);
?>