Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9122167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:02:00+00:00 2026-06-17T06:02:00+00:00

I am attempting to run a variable through several functions to obtain a desired

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T06:02:01+00:00Added an answer on June 17, 2026 at 6:02 am

    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:

    // construct the pipeline
    $valuePlaceholder = new stdClass;
    $pipeline = array(
        // each stage of the pipeline is described by an array
        // where the first element is a callable and the second an array
        // of arguments to pass to that callable
        array('preg_replace', array('~[^\\pL\d]+~u', '-', $valuePlaceholder)),
        array('trim', array($valuePlaceholder, '-')),
        array('iconv', array('utf-8', 'us-ascii//TRANSLIT', $valuePlaceholder)),
        // etc etc
    );
    
    // process it
    $value = $text;
    foreach ($pipeline as $stage) {
        list($callable, $parameters) = $stage;
        foreach ($parameters as &$parameter) {
            if ($parameter === $valuePlaceholder) {
                $parameter = $value;
            }
        }
        $value = call_user_func_array($callable, $parameters);
    }
    
    // final result
    echo $value;
    

    See it in action.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am attempting to run BeepBeep through Mochiweb on Port 80. It works if
When attempting to run the first example in the boost::mpi tutorial , I was
How can I place a variable inside a function when it is run in
Attempting to use squarebracket notation to reference a dynamic variable. (I'm looping through a
When attempting to run the following assembly program: .globl start start: pushq $0x0 movq
I am attempting to run a command as root from a php page, is
I'm attempting to run a very simple Blackberry/Java application which implements the BrowserField class.
I'm attempting to run the default Welcome to ASP.NET MVC! MVC3 application on Azure,
I'm attempting to run kohana classes in the install.php file but am having no
I'm attempting to run a file that calls multiple files, but I'm getting some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.