Skip to content

Fur7/smart_replace

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 

Repository files navigation

Smart replace

A really simple way using the PHP str_replace function with some extra functionalities.

Usage

The difference between str_replace and smart_replace:

$replace = str_replace(["!","Hello"],[".","Good morning"],"Hello world!");
output: Good morning world.

$replace = smart_replace(["!"=>".","Hello"=>"Good morning"],"Hello world!");
output: Good morning world.

Exclude options from the replace function:

For example; To remove a slash from an URL where the link is not starting with /nl/ or /de/

string = '<a href="/nl/testlink"></a> <a href="/testlink"></a> <a href="/de/testlink"></a>';
$searchOn = [
    '<a href="/' => '<a href="'
];
$exclude = [
    '<a href="/nl/',
    '<a href="/de/'
];
$replace = smart_replace($searchOn, $string, $exclude);

output: <a href="/nl/testlink"></a> <a href="testlink"></a> <a href="/de/testlink"></a>

Releases

No releases published

Packages

No packages published

Languages