Search code examples
phpstr-replace

Merge two functions, str_replace?


I need to replace som characters when importing data from a feed. I'm not sure how I can merge two functions into one to make an url correct.

Tried to place one function inside another, but that didn't work.

https://www.xxxxxx.se/brand/[my_custom_function2([my_custom_function3({brand[1]})])]/

How can I combine following two into one?

<?php
function my_custom_function2($string) {
    $string = str_replace (" ", "-", $string);
    return $string;
}
function my_custom_function3($string) {
    $string = str_replace ("Aéryne", "Aeryn", $string);
    return $string;
}
?>

Thanks!

PS. I have hundreds of brands and some is two words. For example "Billi Bi". That is the reason I need the first function.


Solution

  • You could replace several characters using single call by passing array in str_replace

    str_replace (array("Aéryne"," "), array("Aeryn","-"), $string);
    

    Wrap above in single function you don't need n str_replace() calls to replace n characters