ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Replace.php
Go to the documentation of this file.
1 <?php
2 
4 
6 
7 class Replace
8 {
17  public static function replace($oldText, $start, $chars, $newText): string
18  {
19  $oldText = Functions::flattenSingleValue($oldText);
21  $chars = Functions::flattenSingleValue($chars);
22  $newText = Functions::flattenSingleValue($newText);
23 
24  $left = Extract::left($oldText, $start - 1);
25  $right = Extract::right($oldText, Text::length($oldText) - ($start + $chars) + 1);
26 
27  return $left . $newText . $right;
28  }
29 
38  public static function substitute($text = '', $fromText = '', $toText = '', $instance = 0): string
39  {
41  $fromText = Functions::flattenSingleValue($fromText);
42  $toText = Functions::flattenSingleValue($toText);
44 
45  if ($instance == 0) {
46  return str_replace($fromText, $toText, $text);
47  }
48 
49  $pos = -1;
50  while ($instance > 0) {
51  $pos = mb_strpos($text, $fromText, $pos + 1, 'UTF-8');
52  if ($pos === false) {
53  break;
54  }
55  --$instance;
56  }
57 
58  if ($pos !== false) {
59  return self::REPLACE($text, ++$pos, mb_strlen($fromText, 'UTF-8'), $toText);
60  }
61 
62  return $text;
63  }
64 }
static left($value='', $chars=1)
LEFT.
Definition: Extract.php:16
$start
Definition: bench.php:8
static right($value='', $chars=1)
RIGHT.
Definition: Extract.php:62
$text
Definition: errorreport.php:18
static substitute($text='', $fromText='', $toText='', $instance=0)
SUBSTITUTE.
Definition: Replace.php:38
static length($value='')
STRINGLENGTH.
Definition: Text.php:15
static replace($oldText, $start, $chars, $newText)
REPLACE.
Definition: Replace.php:17
static flattenSingleValue($value='')
Convert an array to a single scalar value by extracting the first element.
Definition: Functions.php:649