NOVINKA: Najžiadanejšie rekvalifikačné kurzy teraz s 50% zľavou + kurz AI ZADARMO. Nečakaj, táto ponuka dlho nevydrží! Zisti viac:

PHP - str_replace

Trieda

Koreň \ Bez triedy

Metóda - str_replace

(PHP 4, PHP 5, PHP 7)

Replaces all occurrences of a substring in a string by the given substring.

The search is performed from left to right and we must expect that already replaced characters can be sometimes replaced again. If we want to search case-insensitively, we use the str_ireplace() function.

Procedurálne

  • function str_replace (mixed $search, mixed $replace, mixed $subject, int &$count) : mixed

Parametre

NázovDátový typPredvolená hodnotaPopis
$searchmixed

The substring being searched for.

An array can be used.

$replacemixed

The substring to replace the value.

An array can be used.

$subjectmixed

The string to be replaced.

&$countint

If we specify a variable in this optional parameter, it will store the number of replacements performed.

Mávratovej hodnoty

Vracia: mixed

The function returns the substring after replacing.

Príklady

<?php
echo str_replace('ASP.NET', 'PHP', 'ASP.NET is the best tool for dynamic websites. Let ASP.NET live!');

If we use arrays with substrings, the relevant value from the first array is replaced by a value from the second array. Let's make an example:

<?php
$search =[':)', ':D'];
$replace = ['<img src="happy.png" alt="smile" />', '<img src="laughing.png" alt="laughing" />'];
echo str_replace($search, $replace, 'Hi there :) I am fine because I found ICT :D');

You can see that the function can be used, for example, to replace text emoticons with images. str_replace() is one of the most used functions when working with strings.

Súvisiace manuály

      • function strtr (string $str, string $from, string $to, array $replace_pairs) : string
      Aktivity