Replace

Replace - Replace all occurrences of the search string with the replacement string.

Replace only works with string, any other type is ignored.

Basic usage

use Jawira\Sanitizer\Filters as Sanitizer;

class User {
  #[Sanitizer\Replace(search: ' ', replace: '_')]
  public string $name;
}

Parameters

string search:
The string you want to replace.
string replace:
The replacement string.
string caseSensitive (optional):
Search is case-sensitive, set this parameter to _false_ to perform a case-insensitive search.
Default value is _true_.

Examples

Remove all whitespace characters.

use Jawira\Sanitizer\Filters as Sanitizer;

class User {
  #[Sanitizer\Replace(search: ' ', replace: '')]
  public string $email;
}
"  bob@example.com   " → "bob@example.com"
"bob  @   example.com" → "bob@example.com"
" bob @ example .com " → "bob@example.com"

Replace a string by another.

use Jawira\Sanitizer\Filters as Sanitizer;

class Order {
  #[Sanitizer\Replace('pizza', 'fries')]
  public string $description;
}
"Client wants pizza." → "Client wants fries."

Perform case-insensitive search.

use Jawira\Sanitizer\Filters as Sanitizer;

class Famous {
  #[Sanitizer\Replace('del toro', 'del Toro')]
  public string $name;
}
"Guillermo del toro" → "Guillermo del Toro"
"Guillermo DEL TORO" → "Guillermo del Toro"

See also

StripTags - Strip HTML and PHP tags from a string.