replace

Replace all occurrences of a value in a String, Number, or Array with another value.


Syntax


Parameters

  • Mixed subject - The entity to search.
    • String - The String is searched for any occurrences of search.
    • Number - The Number will be treated as a String.
    • Array - The Array is searched for any items that match search.
  • Mixed search - The value(s) to search for.
    • String - subject is searched for any occurrences of the String.
    • Number - The Number will be treated as a String.
    • Array - If subject is a String, it is searched for any occurrences of any of the items in needle. Otherwise, if subject is an Array, only sub-Arrays that match needle will be found.
    • Boolean - This only has any effect if subject is an Array, in which case, any occurrences will be searched for.
  • Mixed replace - The value(s) to use for replacement.
    • String - All matches of search will be replaced with the String.
    • Number - The Number will be treated as a String.
    • Array - If subject is a String, and search is an Array, all matches will be replaced by the corresponding value in the replace Array - or an empty value if there are no corresponding items. Otherwise, if subject is an Array, replace will be used to replace matching sub-Arrays.
    • Boolean - This only has any effect if subject is an Array, in which case, all matches of search will be replaced.
  • Boolean strict - Whether to employ strict matching.
    • false (default) - If subject is a String, the match will be case-insensitive. Otherwise, if subject is an Array, item types will not be checked, only item values.
    • true - If subject is a String, the match will be case-sensitive. Otherwise, if subject is an Array, item types will have to match as well as values. Array searches are always case-sensitive.


Result

  • Mixed - The modified subject.
    • String - If subject is a String or Number.
    • Array - If subject is an Array.

Returns subject with all occurrences of search replaced with the corresponding replace value.

To count how many matches there are before replacement, search() can be used.

ptpscript/functions/replace.txt · Last modified: 2007/01/24 10:15
 
 
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki