src/php/Webforge/Common/StringUtil.php
public static function lineNumbers($code, $eol = "\n", $begin = 1) {
    $cut = FALSE;
    if (!static::endsWith($code, $eol)) {
      $code .= $eol;
      $cut = -mb_strlen($eol);
    }
    $lines = mb_substr_count($code, $eol);
    $padWhite = mb_strlen((string) $lines); // darstellung der größten zeilen-nummer als string
    $cnt = $begin;
    $linedCode = Preg::replace_callback($code, '/(.*?)'.$eol.'/',
      function ($match) use (&$cnt, $padWhite) {
        return sprintf('%s %s', StringUtil::padRight((string) $cnt++, $padWhite,' '), $match[0]);
      }
    );
    
    if ($cut !== FALSE) {
      return mb_substr($linedCode, 0, $cut);
    } else {
      return $linedCode;
    }
  }
public static function lineNumbers($code, $eol = "\n", $begin = 1) {
    $cut = FALSE;
    if (!static::endsWith($code, $eol)) {
      $code .= $eol;
      $cut = -mb_strlen($eol);
    }
    $lines = mb_substr_count($code, $eol);
    $padWhite = mb_strlen((string) $lines); // darstellung der größten zeilen-nummer als string
    $cnt = $begin;
    $linedCode = Preg::replace_callback($code, '/(.*?)'.$eol.'/',
      function ($match) use (&$cnt, $padWhite) {
        return sprintf('%s %s', StringUtil::padRight((string) $cnt++, $padWhite,' '), $match[0]);
      }
    );
    
    if ($cut !== FALSE) {
      return mb_substr($linedCode, 0, $cut);
    } else {
      return $linedCode;
    }
  }
public static function lineNumbers ( $ code , $ eol = "\n" , $ begin = 1 ) { $ cut = FALSE ; if ( ! static :: endsWith ( $ code , $ eol ) ) { $ code .= $ eol ; $ cut = - mb_strlen ( $ eol ) ; } $ lines = mb_substr_count ( $ code , $ eol ) ; $ padWhite = mb_strlen ( ( string ) $ lines ) ; // darstellung der größten zeilen-nummer als string $ cnt = $ begin ; $ linedCode = Preg :: replace_callback ( $ code , '/(.*?)' . $ eol . '/' , function ( $ match ) use ( & $ cnt , $ padWhite ) { return sprintf ( '%s %s' , StringUtil :: padRight ( ( string ) $ cnt ++ , $ padWhite , ' ' ) , $ match [ 0 ] ) ; } ) ; if ( $ cut !== FALSE ) { return mb_substr ( $ linedCode , 0 , $ cut ) ; } else { return $ linedCode ; } }
Number the lines in the string

@param string $code
@param string $eol the eol from $code
@param int $begin the number of the first line found
@return string
Number the lines in the string