PHP如何从字符串转换或删除特殊字符?

时间:2019-07-04 09:30:46

标签: php wordpress

我想将任何特殊字符转换为它的原始值,并且我正在尝试使用此功能,但是它不起作用。这是我的代码。

  $text = "Tuscaloosa County – Hunting and Timber Invest";

  echo $html = mb_convert_encoding($text, "HTML-ENTITIES", "UTF-8");

4 个答案:

答案 0 :(得分:0)

我不知道那是不是你想要的,但是尝试一下

$text = "Tuscaloosa County – Hunting and Timber Invest"; echo $html =mb_convert_encoding($text, 'UTF-8');

答案 1 :(得分:0)

尝试使用此清理功能

function clean($string) {
       $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.

       return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
    }
     $text = "Tuscaloosa County – Hunting and Timber Invest";

      echo $html = clean($text);

答案 2 :(得分:0)

1 。尝试使用下一个:

 $text = "Tuscaloosa County – Hunting and Timber Invest";
$html_1 = utf8_encode($text);
$html_2 = iconv('ISO-8859-1', 'UTF-8', $text);
$html_2 = mb_convert_encoding($text, 'UTF-8', 'ISO-8859-1');

 $text = "Tuscaloosa County – Hunting and Timber Invest";
$html_1 = utf8_decode($text);
$html_2 = iconv('UTF-8', 'ISO-8859-1', $text);
$html_2 = mb_convert_encoding($text, 'ISO-8859-1', 'UTF-8');

2 。这样:

$html= mb_convert_encode($text,'HTML-ENTITIES','UTF-8'); 

答案 3 :(得分:0)

$new2old = array(
            'á' => 'á','À' => 'À',
            'ä' => 'ä','Ä' => 'Ä','ã' => 'ã','å' => 'Ã¥','Å' => 'Ã…','æ' => 'æ','Æ' => 'Æ',
            'ç' => 'ç','Ç' => 'Ç','é' => 'é','É' => 'É','è' => 'è','È' => 'È','ê' => 'ê',
            'Ê' => 'Ê','ë' => 'ë','Ë' => 'Ë','í' => 'Ã-­­','Í' => 'Ã','ì' => 'ì','Ì' => 'ÃŒ',
            'î' => 'î','Î' => 'ÃŽ','ï' => 'ï','Ï' => 'Ã','ñ' => 'ñ','Ñ' => 'Ñ','ó' => 'ó',
            'Ó' => 'Ó','ò' => 'ò','Ò' => 'Ã’','ô' => 'ô','Ô' => 'Ô','ö' => 'ö','Ö' => 'Ö',
            'õ' => 'õ','Õ' => 'Õ','ø' => 'ø','Ø' => 'Ø','œ' => 'Å“','Œ' => 'Å’','ß' => 'ß',
            'ú' => 'ú','Ú' => 'Ú','ù' => 'ù','Ù' => 'Ù','û' => 'û','Û' => 'Û','ü' => 'ü',
            'Ü' => 'Ãœ','€' => '€','’' => '’','‚' => '‚','ƒ' => 'Æ’','„' => '„','…' => '…',
            '‡' => '‡','ˆ' => 'ˆ','‰' => '‰','Š' => 'Å ','‹' => '‹','Ž' => 'Ž','‘' => '‘',
            '“' => '“','•' => '•','–' => '–','—' => '—','˜' => 'Ëœ','™' => 'â„¢','š' => 'Å¡',
            '›' => '›','ž' => 'ž','Ÿ' => 'Ÿ','¡' => '¡','¢' => '¢','£' => '£','¤' => '¤','¥' => 'Â¥',
            '¦' => '¦','§' => '§','¨' => '¨','©' => '©','ª' => 'ª','«' => '«','¬' => '¬','®' => '®',
            '¯' => '¯','°' => '°','±' => '±','²' => '²','³' => '³','´' => '´','µ' => 'µ','¶' => '¶',
            '·' => '·','¸' => '¸','¹' => '¹','º' => 'º','»' => '»','¼' => '¼','½' => '½','¾' => '¾',
            '¿' => '¿','à' => 'à ','†' => '†','”' => 'â€','Á' => 'Ã','â' => 'â','Â' => 'Â','Ã' => 'Ã',
         );

         foreach( $new2old as $key => $value ) {
                $new[] = $key;
                $old[] = $value;
         }
  $text = "Tuscaloosa County – Hunting and Timber Invest";
  echo $html =$string_new = str_replace( $old, $new, $text );

`