replaceFirst() public static method

The replace first method is *case sensitive*. php StringHelper::replaceFirst('abc', '123', 'abc abc abc'); // returns "123 abc abc"
Since: 1.0.0-rc1
public static replaceFirst ( string $search, string $replace, string $subject ) : mixed
$search string Search string to look for.
$replace string Replacement value for the first found occurrence.
$subject string The string you want to look up to replace the first element.
return mixed Replaced string
Example #1
0
 /**
  * Generate the Link Tag.
  *
  * @param string $value The Brackets value `[]`.
  * @param string $sub The optional Parentheses value `()`
  * @see \luya\tag\TagInterface::parse()
  * @return string The parser tag.
  */
 public function parse($value, $sub)
 {
     if (substr($value, 0, 2) == '//') {
         $value = StringHelper::replaceFirst('//', Url::base(true) . '/', $value);
         $external = false;
     } else {
         $external = true;
     }
     $value = Url::ensureHttp($value);
     $label = empty($sub) ? $value : $sub;
     return Html::a($label, $value, ['class' => $external ? 'link-external' : 'link-internal', 'target' => $external ? '_blank' : null]);
 }
 public function actionClasses()
 {
     if (Config::has('rc1_block_classes_renameing')) {
         return $this->outputError("You already have run the classes updater, so your system should be up-to-date already.");
     }
     foreach (Block::find()->all() as $block) {
         $ns = $block->class;
         foreach ($this->_classMapping as $old => $new) {
             if (StringHelper::startsWith($ns, $old)) {
                 $this->outputError('old: ' . $ns);
                 $newNs = StringHelper::replaceFirst($old, $new, $ns);
                 $block->updateAttributes(['class' => $newNs]);
                 $this->outputSuccess('new: ' . $newNs);
             }
         }
     }
     Config::set('rc1_block_classes_renameing', true);
     return $this->outputSuccess('OK. You can now run the import command.');
 }
Example #3
0
 public function testReplaceFirst()
 {
     $this->assertSame('abc 123 123', StringHelper::replaceFirst('123', 'abc', '123 123 123'));
     $this->assertSame('abc 123 ABC', StringHelper::replaceFirst('ABC', '123', 'abc ABC ABC'));
 }