I am attempting to run a variable through several functions to obtain a desired outcome.
For example, the function to slugify a text works like this:
// replace non letter or digits by -
$text = preg_replace('~[^\\pL\d]+~u', '-', $text);
// trim
$text = trim($text, '-');
// transliterate
$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
// lowercase
$text = strtolower($text);
// remove unwanted characters
$text = preg_replace('~[^-\w]+~', '', $text);
However, we can see that there is a pattern in this example. The $text variable is passed through 5 function calls like this: preg_replace(..., $text) -> trim($text, ...) -> iconv(..., $text) -> strtolower($text) -> preg_replace(..., $text).
Is there a better way we can write the code to allow a variable sieve through several functions?
One way is to write the above code like this:
$text = preg_replace('~[^-\w]+~', '', strtolower(iconv('utf-8', 'us-ascii//TRANSLIT', trim(preg_replace('~[^\\pL\d]+~u', '-', $text), '-'))));
… but this way of writing is a joke and mockery. It hinders code readability.
Since your “function pipeline” is fixed then this is the best (and not coincidentally simplest) way.
If the pipeline were to be dynamically constructed then you could do something like:
See it in action.