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

  • Home
  • SEARCH
  • 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 6849259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:58:07+00:00 2026-05-27T00:58:07+00:00

str_repeat(A, B) repeat string A , B times: $string = This is a .

  • 0

str_repeat(A, B) repeat string A, B times:

$string = "This is a " . str_repeat("test", 2) . 
          "! " . str_repeat("hello", 3) . " and Bye!";  

// Return "This is a testtest! hellohellohello and Bye!"

I need reverse operation:

str_shrink($string, array("hello", "test")); 
// Return "This is a test(x2)! hello(x3) and Bye!" or
//        "This is a [test]x2! [hello]x3 and Bye!"

Best and efficient way for create str_shrink function?

  • 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-05-27T00:58:08+00:00Added an answer on May 27, 2026 at 12:58 am

    Here are two versions that I could come up with.

    The first uses a regular expression and replaces duplicate matches of the $needle string with a single $needle string. This is the most vigorously tested version and handles all possibilities of inputs successfully (as far as I know).

    function str_shrink( $str, $needle)
    {
        if( is_array( $needle))
        {
            foreach( $needle as $n)
            {
                $str = str_shrink( $str, $n);   
            }
            return $str;
        }
        $regex = '/(' . $needle . ')(?:' . $needle . ')+/i';
        return preg_replace_callback( $regex, function( $matches) { return $matches[1] . '(x' . substr_count( $matches[0], $matches[1]) . ')'; }, $str);
    }
    

    The second uses string manipulation to continually replace occurrences of the $needle concatenated with itself. Note that this one will fail if $needle.$needle occurs more than once in the input string (The first one does not have this problem).

    function str_shrink2( $str, $needle)
    {
        if( is_array( $needle))
        {
            foreach( $needle as $n)
            {
                $str = str_shrink2( $str, $n);   
            }
            return $str;
        }
        $count = 1; $previous = -1;
        while( ($i = strpos( $str, $needle.$needle)) > 0)
        {
            $str = str_replace( $needle.$needle, $needle, $str);
            $count++;
            $previous = $i;
        }
        if( $count > 1)
        {
            $str = substr( $str, 0, $previous) . $needle .'(x' . $count . ')' . substr( $str, $previous + strlen( $needle));
        }
        return $str;
    }
    

    See them both in action

    Edit: I didn’t realize that the desired output wanted to include the number of repetitions. I’ve modified my examples accordingly.

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

Sidebar

Related Questions

char *str = NULL; str = Operation(argv[1]); cout << Cal(str); Operation function return a
I need to check if a string starts with the following: += I've tried
I want to format a string with sprintf but repeating many times an argument.
I am coding a Drupal payment method module and within this I need to
In php, unpack() has the * flag which means repeat this format until the
Here's my code: $long = str_repeat('a very long string text', 100); // try changing
I am basically just wondering how can I make this while loop repeat continuously
I would like to replace a char in a string. My idea was this:
str_replace('Ê','',$line); Ain't working. Is there some special string that represents that?
string str = one three; string::iterator it; string add = two ; Lets say

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.