URL'den tüm özel karakterleri nasıl kaldırılır?

4 Cevap php

Benim dersim var

public function convert( $title )
    {
        $nameout = strtolower( $title );
        $nameout = str_replace(' ', '-', $nameout );
        $nameout = str_replace('.', '', $nameout);
        $nameout = str_replace('æ', 'ae', $nameout);
        $nameout = str_replace('ø', 'oe', $nameout);
        $nameout = str_replace('å', 'aa', $nameout);
        $nameout = str_replace('(', '', $nameout);
        $nameout = str_replace(')', '', $nameout);
        $nameout = preg_replace("[^a-z0-9-]", "", $nameout);    

        return $nameout;
    }

Ama sombody burada bana yardımcı olabilir, ben ö gibi özel karakterleri kullanmak ve ü ve diğer zaman işe alınamıyor? PHP 5.3 kullanın.

4 Cevap

In ilk cevap this SO thread bunu yapmak için gereken kod içerir.

Ve ne:

<?php
$query_string = 'foo=' . urlencode($foo) . '&bar=' . urlencode($bar);
echo '<a href="mycgi?' . htmlentities($query_string) . '">';
?>

Gönderen: http://php.net/manual/en/function.urlencode.php

Ben üzerinde çalıştığım bir proje için bir süre önce bu fonksiyonu yazdı ve RegEx çalışmak için alamadım. Onun en iyi yolu değil, ama çalışıyor.

function safeURL($input){
    $input = strtolower($input);
    for($i = 0; $i < strlen($input); $i++){
        $working = ord(substr($input,$i,1));
        if(($working>=97)&&($working<=122)){
            //a-z
            $out = $out . chr($working);
        } elseif(($working>=48)&&($working<=57)){
            //0-9
            $out = $out . chr($working);
        } elseif($working==46){
            //.
            $out = $out . chr($working);
        } elseif($working==45){
            //-
            $out = $out . chr($working);
        }
    }
    return $out;
}

Here's a function to help with what you're doing, it's written in Czech: http://php.vrana.cz/vytvoreni-pratelskeho-url.php (and translated to English)

İşte bu başka almak bulunuyor (from the Symfony documentation):

<?php 
function slugify($text)
{
  // replace non letter or digits by -
  $text = preg_replace('~[^\\pL\d]+~u', '-', $text);

  // trim
  $text = trim($text, '-');

  // transliterate
  if (function_exists('iconv'))
  {
    $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
  }

  // lowercase
  $text = strtolower($text);

  // remove unwanted characters
  $text = preg_replace('~[^-\w]+~', '', $text);

  if (empty($text))
  {
    return 'n-a';
  }

  return $text;
}