str::replace($string, $search, $replace, $limit = -1)

Replaces all or some occurrences of the search string with the replacement string. Extension of the str_replace() function in PHP with an additional $limit parameter.

  • since: Kirby 2.5.9
  • $string (string|array)
    String being replaced on (haystack); can be an array of multiple subject strings
  • $search (string|array)
    Value being searched for (needle)
  • $replace (string|array)
    Value to replace matches with
  • $limit (int|array)
    Maximum possible replacements for each search value; multiple limits for each search value are supported; defaults to no limit
  • return (string|array)
    String with replaced values; if $string is an array, array of strings

Example

<?php

echo str::replace('some string', 'some', 'other');

// output: other string
<?php

// only replace two times
echo str::replace('abcabcabc', 'a', '!', 2);

// output: !bc!bcabc