luya\helpers\StringHelper::replaceFirst PHP Méthode

replaceFirst() public static méthode

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.
Résultat mixed Replaced string
    public static function replaceFirst($search, $replace, $subject)
    {
        return preg_replace('/' . preg_quote($search, '/') . '/', $replace, $subject, 1);
    }

Usage Example

Exemple #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]);
 }
All Usage Examples Of luya\helpers\StringHelper::replaceFirst