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 7811823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:18:27+00:00 2026-06-02T04:18:27+00:00

I can do this using explode (or have fun with with strrpos ), but

  • 0

I can do this using explode (or have fun with with strrpos), but i prefer to using preg_replace because should be a bit faster, i think (isn’t it?). Plus, it’s concise and elegant.

The purpose is, given a string like a_b_c to obtain another sting where characters following the last _ where substituted with passed string.

I’m not good at regular expression. I’ve to find the time to buy a good book at study. Anyway i’ve tried this regex '/_(.*)$/' as match the end of the string, capture any character following the last underscore.

What’s wrong in my argumentation?

// Do it using explode
function foo($string, $replacement)
{
    $pieces = explode('_', $string);
    array_pop($pieces);
    return implode('_', array_merge($pieces, array($replacement)));
}

// Do it using regular expression (not working)
function bar($string, $replacement)
{
    return preg_replace('/_(.*)$/', $replacement, $string);
}

echo foo('a_b_c', 3); // Prints a_b_3
echo bar('a_b_c', 3); // Prints a3 wrong!!!
  • 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-02T04:18:29+00:00Added an answer on June 2, 2026 at 4:18 am

    Depending on your common search string and your version of PCRE, either preg_replace or strrpos will probably fare best:

    Functions

    function usingExplode($string, $replacement) {
        $pieces = explode('_', $string);
        array_pop($pieces);
        return implode('_', array_merge($pieces, array($replacement)));
    }
    
    function usingStrrpos($string, $replacement) {
        return substr($string, 0, strrpos($string, '_') + 1) . $replacement;
    }
    
    function usingPreg($string, $replacement) {
        return preg_replace('/_[^_]*$/', '_' . $replacement, $string);
    }
    

    Test harness

    function speedTest($function, $string, $count = 100000) {
        $start = microtime(true);
    
        for ($i = 0; $i < $count; $i++) {
            $function($string, 'replacement');
        }
    
        $end = microtime(true);
    
        printf('%-12s: %01.2fs%s', $function, $end - $start, PHP_EOL);
    }
    
    $tests = array('a_b_c', 'abcdefghijklmnopqrstuvwxy_z', 'a_bcdefghijklmnopqrstuvwxyz', 'a_b_c_d_e_f_g_h_i_j_k_l_m_n_o_p_q_r_s_t_u_v_w_x_y_z');
    
    foreach ($tests as $test) {
        echo $test . ':' . PHP_EOL;
        speedTest('usingExplode', $test);
        speedTest('usingStrrpos', $test);
        speedTest('usingPreg',    $test);
        echo PHP_EOL;
    }
    

    Results

    a_b_c:
    usingExplode: 0.64s
    usingStrrpos: 0.34s
    usingPreg   : 0.33s
    
    abcdefghijklmnopqrstuvwxy_z:
    usingExplode: 0.61s
    usingStrrpos: 0.32s
    usingPreg   : 0.32s
    
    a_bcdefghijklmnopqrstuvwxyz:
    usingExplode: 0.60s
    usingStrrpos: 0.32s
    usingPreg   : 0.32s
    
    a_b_c_d_e_f_g_h_i_j_k_l_m_n_o_p_q_r_s_t_u_v_w_x_y_z:
    usingExplode: 1.39s
    usingStrrpos: 0.32s
    usingPreg   : 0.71s
    

    Note that (at least on my set-up with PHP 5.4.0), preg_replace gives strrpos a run for its money until there are a large number of underscores preceding the last one.

    EDIT: I plugged bfrohs’s regex into the suite and unless the underscore to replace is near the beginning of the string, it doesn’t do very well:

    a_b_c:
    usingPreg2: 0.40s
    
    abcdefghijklmnopqrstuvwxy_z:
    usingPreg2: 1.91s
    
    a_bcdefghijklmnopqrstuvwxyz:
    usingPreg2: 0.38s
    
    a_b_c_d_e_f_g_h_i_j_k_l_m_n_o_p_q_r_s_t_u_v_w_x_y_z:
    usingPreg2: 1.04s
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can do this easily using a TextBoxFor but how do I do it
I want DBSession.query(Article).group_by(Article.created.month).all() But this query can't using How do I do this using
I have an array that is built using the explode() function, but seeing how
This is not the explode can solve I have several array $order data: {5,3,2}
I am using http://uniformjs.com for my html controls. We can apply this using code
Using this: Can I use Facebook's fb:friend-selector in an iframe? I created a multi-friend
How can I do this using CSS and HTML? Bordered text surrounded by colored
Not sure if yall can help this time, as I'm just using this particular
Given this XML: <Items> <Item>a</Item> <Item>b</Item> </Items> How can I deserialize this using XmlSerializer
[1,2,3].forEach(function(el) { if(el === 1) break; }); How can I do this using the

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.