當前位置: 首頁>>代碼示例>>PHP>>正文


PHP Unicode::validateUtf8方法代碼示例

本文整理匯總了PHP中Drupal\Component\Utility\Unicode::validateUtf8方法的典型用法代碼示例。如果您正苦於以下問題:PHP Unicode::validateUtf8方法的具體用法?PHP Unicode::validateUtf8怎麽用?PHP Unicode::validateUtf8使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Drupal\Component\Utility\Unicode的用法示例。


在下文中一共展示了Unicode::validateUtf8方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。

示例1: testValidateUtf8

 /**
  * Tests UTF-8 validation.
  *
  * @dataProvider providerTestValidateUtf8
  * @covers ::validateUtf8
  *
  * @param string $text
  *   The text to validate.
  * @param boolean $expected
  *   The expected return value from Unicode::validateUtf8().
  * @param string $message
  *   The message to display on failure.
  */
 public function testValidateUtf8($text, $expected, $message)
 {
     $this->assertEquals($expected, Unicode::validateUtf8($text), $message);
 }
開發者ID:Nikola-xiii,項目名稱:d8intranet,代碼行數:17,代碼來源:UnicodeTest.php

示例2: extractMollomValues


//.........這裏部分代碼省略.........
         $data['postId'] = $mapping['post_id'];
     }
     // Post title.
     if (!empty($mapping['post_title'])) {
         $data['postTitle'] = $mapping['post_title'];
     }
     // Post body.
     if (!empty($post_body)) {
         $data['postBody'] = $post_body;
     }
     // Author ID.
     // If a non-anonymous user ID was mapped via form values, use that.
     if (!empty($mapping['author_id'])) {
         $data['authorId'] = $mapping['author_id'];
     } elseif (!empty($user->id())) {
         $data['authorId'] = $user->id();
     }
     // Load the user account of the author, if any, for the following author*
     // property assignments.
     $account = FALSE;
     if (isset($data['authorId'])) {
         /** @var \Drupal\user\Entity\User $account */
         $account = User::load($data['authorId']);
         $author_username = $account->getUsername();
         $author_email = $account->getEmail();
         // Author creation date.
         $data['authorCreated'] = $account->getCreatedTime();
         // In case a post of a registered user is edited and a form value mapping
         // exists for author_id, but no form value mapping exists for author_name,
         // use the name of the user account associated with author_id.
         // $account may be the same as the currently logged-in $user at this point.
         if (!empty($author_username)) {
             $data['authorName'] = $author_username;
         }
         if (!empty($author_email)) {
             $data['authorMail'] = $author_email;
         }
     }
     // Author name.
     // A form value mapping always has precedence.
     if (!empty($mapping['author_name'])) {
         $data['authorName'] = $mapping['author_name'];
     }
     // Author e-mail.
     if (!empty($mapping['author_mail'])) {
         $data['authorMail'] = $mapping['author_mail'];
     }
     // Author homepage.
     if (!empty($mapping['author_url'])) {
         $data['authorUrl'] = $mapping['author_url'];
     }
     // Author OpenID.
     if (!empty($mapping['author_openid'])) {
         $data['authorOpenid'] = $mapping['author_openid'];
     }
     // Author IP.
     $data['authorIp'] = \Drupal::request()->getClientIp();
     $mollom_form = $form_state->getValue('mollom');
     // Honeypot.
     // For the Mollom backend, it only matters whether 'honeypot' is non-empty.
     // The submitted value is only taken over to allow site administrators to
     // see the actual honeypot value in watchdog log entries.
     if (isset($mollom_form['homepage']) && $mollom_form['homepage'] !== '') {
         $data['honeypot'] = $mollom_form['homepage'];
     }
     // Add the contextCreated parameter if a callback exists.
     if (isset($mollom_form['context created callback']) && function_exists($mollom_form['context created callback'])) {
         if (!empty($mapping['context_id'])) {
             $contextCreated = call_user_func($mollom_form['context created callback'], $mapping['context_id']);
             if ($contextCreated !== FALSE) {
                 $data['contextCreated'] = $contextCreated;
             }
         }
     }
     // Ensure that all $data values contain valid UTF-8. Invalid UTF-8 would be
     // sanitized into an empty string, so the Mollom backend would not receive
     // any value.
     $invalid_utf8 = FALSE;
     $invalid_xml = FALSE;
     // Include the CAPTCHA solution user input in the UTF-8 validation.
     $solution = isset($mollom_form['captcha']['captcha_input']) ? array('solution' => $mollom_form['captcha']['captcha_input']) : array();
     foreach ($data + $solution as $key => $value) {
         // Check for invalid UTF-8 byte sequences first.
         if (!Unicode::validateUtf8($value)) {
             $invalid_utf8 = TRUE;
             // Replace the bogus string, since $data will be logged as
             // check_plain(var_export($data)), and check_plain() would empty the
             // entire exported variable string otherwise.
             $data[$key] = '- Invalid UTF-8 -';
         } elseif (preg_match('@[^\\x9\\xA\\xD\\x20-\\x{D7FF}\\x{E000}-\\x{FFFD}\\x{10000}-\\x{10FFFF}]@u', $value)) {
             $invalid_xml = TRUE;
         }
     }
     if ($invalid_utf8 || $invalid_xml) {
         $form_state->setErrorByName('', t('Your submission contains invalid characters and will not be accepted.'));
         Logger::addMessage(['message' => 'Invalid @type in form values', 'arguments' => ['@type' => $invalid_utf8 ? 'UTF-8' : 'XML characters'], 'data' => $data]);
         $data = FALSE;
     }
     return $data;
 }
開發者ID:isramv,項目名稱:camp-gdl,代碼行數:101,代碼來源:FormController.php


注:本文中的Drupal\Component\Utility\Unicode::validateUtf8方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。