Hướng dẫn php remove hidden characters - php xóa các ký tự ẩn

Tôi đang gặp khó khăn để phù hợp với hai chuỗi văn bản. Một chứa một số ký tự ẩn từ chuỗi văn bản.

Tôi có một chuỗi văn bản: "PR & Communications" được lưu trữ trên cơ sở dữ liệu SQL. Khi được kéo từ đó, vào $database_version, var_dump($database_version) cho thấy chuỗi có 19 byte.

Tôi đã quét (với sự cho phép) từ một trang web, một số văn bản thành một biến, $web_version. Rõ ràng chuỗi là "PR & Communications" nhưng nó không khớp với phiên bản cơ sở dữ liệu, tức là if($database_version == $web_version) không đúng.

var_dump() cho thấy $web_version có 23 byte. trim() không có tác dụng, cũng không ____10 nhưng

// Remove all characters except A-Z, a-z, 0-9, dots, hyphens and spaces
// Note that the hyphen must go last not to be confused with a range (A-Z)
// and the dot, NOT being special (I know. My life was a lie), is NOT escaped

$str = preg_replace('/[^A-Za-z0-9. -]/', '', $str);

// Replace sequences of spaces with hyphen
$str = preg_replace('/  */', '-', $str);

// The above means "a space, followed by a space repeated zero or more times"
// (should be equivalent to / +/)

// You may also want to try this alternative:
$str = preg_replace('/\\s+/', '-', $str);

// where \s+ means "zero or more whitespaces" (a space is not necessarily the
// same as a whitespace) just to be sure and include everything
1 sẽ loại bỏ một cái gì đó vì sau đó
// Remove all characters except A-Z, a-z, 0-9, dots, hyphens and spaces
// Note that the hyphen must go last not to be confused with a range (A-Z)
// and the dot, NOT being special (I know. My life was a lie), is NOT escaped

$str = preg_replace('/[^A-Za-z0-9. -]/', '', $str);

// Replace sequences of spaces with hyphen
$str = preg_replace('/  */', '-', $str);

// The above means "a space, followed by a space repeated zero or more times"
// (should be equivalent to / +/)

// You may also want to try this alternative:
$str = preg_replace('/\\s+/', '-', $str);

// where \s+ means "zero or more whitespaces" (a space is not necessarily the
// same as a whitespace) just to be sure and include everything
2 cho thấy chuỗi chỉ bao gồm 14 byte. Nó rõ ràng đã loại bỏ một cái gì đó, có thể là quá nhiều, vì chuỗi vẫn không khớp với $database_version.

Bất kỳ ý tưởng làm thế nào để:

  1. Tìm hiểu những gì đã bị xóa
  2. Dải ra vừa đủ để phù hợp với $ database_version?

Tái bút Tôi không biết làm thế nào để xem biến trong mã thập lục phân

Cập nhật

Giải pháp dưới đây có phiên bản "SEO thân thiện hơn":

Nội phân Chính showShow

  • Câu trả lời trước
  • Làm cách nào để loại bỏ các ký tự và không gian đặc biệt khỏi chuỗi?
  • Làm cách nào để loại bỏ số và ký tự đặc biệt khỏi chuỗi trong PHP?
  • Làm thế nào tôi có thể thay thế các ký tự đặc biệt trong một chuỗi trong PHP?
  • Làm thế nào loại bỏ không gian bên trái và bên phải trong PHP?

function hyphenize($string) {
    $dict = array(
        "I'm"      => "I am",
        "thier"    => "their",
        // Add your own replacements here
    );
    return strtolower(
        preg_replace(
          array( '#[\\s-]+#', '#[^A-Za-z0-9. -]+#' ),
          array( '-', '' ),
          // the full cleanString() can be downloaded from http://www.unexpectedit.com/php/php-clean-string-of-utf8-chars-convert-to-similar-ascii-char
          cleanString(
              str_replace( // preg_replace can be used to support more complicated replacements
                  array_keys($dict),
                  array_values($dict),
                  urldecode($string)
              )
          )
        )
    );
}

function cleanString($text) {
    $utf8 = array(
        '/[áàâãªä]/u'   =>   'a',
        '/[ÁÀÂÃÄ]/u'    =>   'A',
        '/[ÍÌÎÏ]/u'     =>   'I',
        '/[íìîï]/u'     =>   'i',
        '/[éèêë]/u'     =>   'e',
        '/[ÉÈÊË]/u'     =>   'E',
        '/[óòôõºö]/u'   =>   'o',
        '/[ÓÒÔÕÖ]/u'    =>   'O',
        '/[úùûü]/u'     =>   'u',
        '/[ÚÙÛÜ]/u'     =>   'U',
        '/ç/'           =>   'c',
        '/Ç/'           =>   'C',
        '/ñ/'           =>   'n',
        '/Ñ/'           =>   'N',
        '/–/'           =>   '-', // UTF-8 hyphen to "normal" hyphen
        '/[’‘‹›‚]/u'    =>   ' ', // Literally a single quote
        '/[“”«»„]/u'    =>   ' ', // Double quote
        '/ /'           =>   ' ', // nonbreaking space (equiv. to 0x160)
    );
    return preg_replace(array_keys($utf8), array_values($utf8), $text);
}

Hàm Trim () sẽ loại bỏ khoảng trắng và các ký tự được xác định trước khác từ cả hai phía của chuỗi. Các hàm liên quan: LTRIM () - Loại bỏ khoảng trắng hoặc các ký tự được xác định trước khác khỏi phía bên trái của chuỗi. RTRIM () - Loại bỏ khoảng trắng hoặc các ký tự được xác định trước khác khỏi phía bên phải của chuỗi.

Cập nhậtBogotà became bogot, Medellìn became medelln and so on).

Ngoài ra còn có một số lỗi chính tả phổ biến dường như ảnh hưởng đến kết quả, và lời giải thích duy nhất có ý nghĩa với tôi là URL của chúng tôi đã được giải nén, những từ ngữ được phát ra và thường lái xe biết các thuật toán xếp hạng nào. Và những thuật toán đó rõ ràng đã được cho ăn bằng các chuỗi được làm sạch UTF8, do đó "perù" trở thành "peru" thay vì "mỗi". "Per" không phù hợp và loại nó vào cổ.

Để cả giữ các ký tự UTF8 và thay thế một số lỗi chính tả, chức năng nhanh hơn bên dưới đã trở thành hàm chính xác hơn (?) Ở trên.

// Remove all characters except A-Z, a-z, 0-9, dots, hyphens and spaces
// Note that the hyphen must go last not to be confused with a range (A-Z)
// and the dot, NOT being special (I know. My life was a lie), is NOT escaped

$str = preg_replace('/[^A-Za-z0-9. -]/', '', $str);

// Replace sequences of spaces with hyphen
$str = preg_replace('/  */', '-', $str);

// The above means "a space, followed by a space repeated zero or more times"
// (should be equivalent to / +/)

// You may also want to try this alternative:
$str = preg_replace('/\\s+/', '-', $str);

// where \s+ means "zero or more whitespaces" (a space is not necessarily the
// same as a whitespace) just to be sure and include everything
6 cần phải được thiết kế bằng tay, tất nhiên.

Câu trả lời trước

Một cách tiếp cận đơn giản:

// Remove all characters except A-Z, a-z, 0-9, dots, hyphens and spaces
// Note that the hyphen must go last not to be confused with a range (A-Z)
// and the dot, NOT being special (I know. My life was a lie), is NOT escaped

$str = preg_replace('/[^A-Za-z0-9. -]/', '', $str);

// Replace sequences of spaces with hyphen
$str = preg_replace('/  */', '-', $str);

// The above means "a space, followed by a space repeated zero or more times"
// (should be equivalent to / +/)

// You may also want to try this alternative:
$str = preg_replace('/\\s+/', '-', $str);

// where \s+ means "zero or more whitespaces" (a space is not necessarily the
// same as a whitespace) just to be sure and include everything

Lưu ý rằng bạn có thể phải trước tiên

// Remove all characters except A-Z, a-z, 0-9, dots, hyphens and spaces
// Note that the hyphen must go last not to be confused with a range (A-Z)
// and the dot, NOT being special (I know. My life was a lie), is NOT escaped

$str = preg_replace('/[^A-Za-z0-9. -]/', '', $str);

// Replace sequences of spaces with hyphen
$str = preg_replace('/  */', '-', $str);

// The above means "a space, followed by a space repeated zero or more times"
// (should be equivalent to / +/)

// You may also want to try this alternative:
$str = preg_replace('/\\s+/', '-', $str);

// where \s+ means "zero or more whitespaces" (a space is not necessarily the
// same as a whitespace) just to be sure and include everything
7 URL, vì%20 và + cả hai đều thực sự là không gian-ý tôi là, nếu bạn có "không bao giờ%20gonna%20Give%20you%20up" Bạn muốn nó trở thành không bao giờ gonna-give-bạn -Up, không bao giờ 20gonna20give20you20up. Bạn có thể không cần nó, nhưng tôi nghĩ rằng tôi sẽ đề cập đến khả năng.

Vì vậy, chức năng đã hoàn thành cùng với các trường hợp thử nghiệm:

function hyphenize($string) {
    return 
    ## strtolower(
          preg_replace(
            array('#[\\s-]+#', '#[^A-Za-z0-9. -]+#'),
            array('-', ''),
        ##     cleanString(
              urldecode($string)
        ##     )
        )
    ## )
    ;
}

print implode("\n", array_map(
    function($s) {
            return $s . ' becomes ' . hyphenize($s);
    },
    array(
    'Never%20gonna%20give%20you%20up',
    "I'm not the man I was",
    "'Légeresse', dit sa majesté",
    )));


Never%20gonna%20give%20you%20up    becomes  never-gonna-give-you-up
I'm not the man I was              becomes  im-not-the-man-I-was
'Légeresse', dit sa majesté        becomes  legeresse-dit-sa-majeste

Để xử lý UTF-8, tôi đã sử dụng triển khai

// Remove all characters except A-Z, a-z, 0-9, dots, hyphens and spaces
// Note that the hyphen must go last not to be confused with a range (A-Z)
// and the dot, NOT being special (I know. My life was a lie), is NOT escaped

$str = preg_replace('/[^A-Za-z0-9. -]/', '', $str);

// Replace sequences of spaces with hyphen
$str = preg_replace('/  */', '-', $str);

// The above means "a space, followed by a space repeated zero or more times"
// (should be equivalent to / +/)

// You may also want to try this alternative:
$str = preg_replace('/\\s+/', '-', $str);

// where \s+ means "zero or more whitespaces" (a space is not necessarily the
// same as a whitespace) just to be sure and include everything
8 được tìm thấy trực tuyến (liên kết bị hỏng kể từ đó, nhưng một bản sao bị tước bỏ với tất cả các ký tự UTF8 không quá như vậy ở đầu câu trả lời; Bạn cần) chuyển đổi các ký tự UTF8 thành các ký tự bình thường, do đó bảo tồn từ "nhìn" càng nhiều càng tốt. Nó có thể được đơn giản hóa và được bao bọc bên trong chức năng ở đây để thực hiện.

Chức năng trên cũng thực hiện chuyển đổi sang chữ thường - nhưng đó là một hương vị. Mã để làm như vậy đã được bình luận.

Làm cách nào để loại bỏ các ký tự và không gian đặc biệt khỏi chuỗi?

Sử dụng phương thức thay thế () để xóa tất cả các ký tự đặc biệt khỏi chuỗi, ví dụ: str. thay thế (/[^a-za-z0-9]/g, ''); . Phương thức thay thế sẽ trả về một chuỗi mới không chứa bất kỳ ký tự đặc biệt nào., e.g. str. replace(/[^a-zA-Z0-9 ]/g, ''); . The replace method will return a new string that doesn't contain any special characters.

Làm cách nào để loại bỏ số và ký tự đặc biệt khỏi chuỗi trong PHP?

1 câu trả lời. Hiển thị hoạt động trên bài viết này. hàm sạch ($ chuỗi) {$ string = str_replace ('', '-', $ string); // thay thế tất cả các khoảng trắng bằng hyphens.return preg_replace ('/[^a-za-z \-]/', '', $ String); // Loại bỏ các ký tự đặc biệt. }function clean($string) { $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens. return preg_replace('/[^A-Za-z\-]/', '', $string); // Removes special chars. }

Làm thế nào tôi có thể thay thế các ký tự đặc biệt trong một chuỗi trong PHP?

Hàm str_replace () thay thế một số ký tự bằng một số ký tự khác trong một chuỗi. Hàm này hoạt động theo các quy tắc sau: Nếu chuỗi được tìm kiếm là một mảng, nó sẽ trả về một mảng. Nếu chuỗi được tìm kiếm là một mảng, hãy tìm và thay thế được thực hiện với mọi phần tử mảng. replaces some characters with some other characters in a string. This function works by the following rules: If the string to be searched is an array, it returns an array. If the string to be searched is an array, find and replace is performed with every array element.

Làm thế nào loại bỏ không gian bên trái và bên phải trong PHP?

Hàm Trim () sẽ loại bỏ khoảng trắng và các ký tự được xác định trước khác từ cả hai phía của chuỗi. Các hàm liên quan: LTRIM () - Loại bỏ khoảng trắng hoặc các ký tự được xác định trước khác khỏi phía bên trái của chuỗi. RTRIM () - Loại bỏ khoảng trắng hoặc các ký tự được xác định trước khác khỏi phía bên phải của chuỗi.trim() function removes whitespace and other predefined characters from both sides of a string. Related functions: ltrim() - Removes whitespace or other predefined characters from the left side of a string. rtrim() - Removes whitespace or other predefined characters from the right side of a string.