You are here

public static function TextUtility::unicodePregMatch in Image Effects 8

Same name and namespace in other branches
  1. 8.3 src/Component/TextUtility.php \Drupal\image_effects\Component\TextUtility::unicodePregMatch()
  2. 8.2 src/Component/TextUtility.php \Drupal\image_effects\Component\TextUtility::unicodePregMatch()

Unicode-safe preg_match().

Search subject for a match to the regular expression given in pattern, but return offsets in characters, where preg_match would return offsets in bytes.

See also

http://php.net/manual/en/function.preg-match.php

http://drupal.org/node/465638

3 calls to TextUtility::unicodePregMatch()
TextToWrapper::wrapText in src/Plugin/ImageToolkit/Operation/gd/TextToWrapper.php
Wrap text for rendering at a given width.
TextUtilityTest::testCapturedOffset in tests/src/Unit/TextUtilityTest.php
Performs the tests for the captured offset.
TextUtilityTest::testOffsetArgument in tests/src/Unit/TextUtilityTest.php
Performs the tests for the offset argument.

File

src/Component/TextUtility.php, line 51

Class

TextUtility
Text handling methods for image_effects.

Namespace

Drupal\image_effects\Component

Code

public static function unicodePregMatch($pattern, $subject, &$matches, $flags = NULL, $offset = 0) {

  // Convert the offset value from characters to bytes.
  // NOTE - strlen is used on purpose here to get string length in bytes.
  // @see https://www.drupal.org/node/465638#comment-1600860
  $offset = strlen(Unicode::substr($subject, 0, $offset));
  $return_value = preg_match($pattern, $subject, $matches, $flags, $offset);
  if ($return_value && $flags & PREG_OFFSET_CAPTURE) {
    foreach ($matches as &$match) {

      // Convert the offset returned by preg_match from bytes back to
      // characters.
      // NOTE - substr is used on purpose here to get offset in bytes.
      // @see https://www.drupal.org/node/465638#comment-1600860
      $match[1] = Unicode::strlen(substr($subject, 0, $match[1]));
    }
  }
  return $return_value;
}