10 December 2015

Repeat a string in PHP with iteration counter - quick and easy function

When you're working through some complex program logic, low level code like manipulating strings is a distraction to write and worse, when you come back to it six months later, it makes your code look long and daunting to get back into. With this in mind, I always try to wrap low level code in re-usable functions.

Here's one such handy little function which works just like PHP's native str_repeat but it will include the iteration count in the string itself and do some cleaning at the end. This is perfect for generating numbered lists, sql queries and a bunch of other uses.




//Repeat a string with iterator
function strRepeat( $string, $count=1, $replace='', $trim='' ){
 $res = '';
 for($i=1; $i<=$count; $i++) $res .= str_replace($replace,$i,$string);
 return $trim ? trim($res,$trim) : $res;
}

//EXAMPLE:
echo strRepeat('testX,',5,'X',',');

// outputs: "test1,test2,test3,test4,test5" //note the final , is trimmed away, ready for use

14 comments:

Anonymous said...

We love the fact that you are discussing technology in a very refined manner. The perspective on this issue is very enlightening. Great job. Urban Kreative - website designing and developing company in Nairobi Kenya http://www.urbankreative.com

محمد على said...




شركة تنظيف مسابح بالرياض
شركة تنظيف واجهات زجاج بالرياض
افضل شركة مكافحة حشرات بالرياض
شركة رش مبيدات بالرياض


Sahithya said...

It's a useful blog. Enroll today to get free access to our live demo session which is a great opportunity to interact with the trainer directly which is a placement based Salesforce training India with job placement and certification . Get salesforce training in affordable cost from a best computer institute.

مجدى said...

شركة تنظيف شقق بالرياض
شركة تنظيف موكيت بالرياض
شركة تنظيف واجهات زجاج بالرياض

sara laurence said...

If you are looking for the best PHP and other web development services than I would recommend native web studio as they have the best team of professional developers and digital marketers who can give you the best digital services.

sara laurence said...

I have no idea about the PHP but I can refer you my best CBD products because all the can natural CBD because all our products are made by natural herbs.

Anonymous said...

Link 12bet - Vie Casino
Link 12bet. 1X2.5.2, MTT, H2H2H2H2W, KH2H2H2H, H2H2H2H2H. Link - Link Link - Link- Link- Link- Link- Link- Link- Link- Link- Link- Link- Link- Link- Link- Link- Link- jeetwin Link- Link- Link- Link- Link- Link- Link- Link- Link- Link- Link- Link- Link- Link- Link- Link- Link- Link- Link- planet win 365 Link- Link- Link- Link- Link- Link- Link- Link- Link- link 12bet Link- Link- Link- Link- Link- Link- Link- Link- Link- Link- Link- Link- Link- Link- Link- Link- Link- Link- Link- Link- Link- Link

sara laurence said...

For the PHP services we have a lot of options available but for the book writing I would like to suggest you royal book writers as they are providing the best book writing services.

Anonymous said...

Such an interesting article about the PHP but just like to suggest about the best online logo design services through which you can get the best logo designing services.

johnass said...

I appreciate you giving such lovely content. I learned something from your blog. Continue sharing.
divorce lawyer new jersey

frozen cheesecake said...

Every time I search for info, I end up at your blog! You’re amazing at what you do. Thanks for the great content!
frozen cheesecake

grocery store champaign il said...

Nice explanation of repeating strings using iteration—this is especially helpful for understanding what’s happening under the hood instead of relying only on built-in functions like str_repeat(), which simply returns a string repeated a given number of times
grocery store champaign il

common crossbill said...

Great explanation and a very practical PHP tip! The iterative approach for repeating strings is simple, clean, and easy for developers to implement in real projects. Thanks for sharing this useful tutorial.

common crossbill

bakery springfield il said...

Nice and helpful explanation of repeating strings in PHP using iteration. The examples are simple, easy to understand, and very useful for beginners learning loops and string handling in PHP.
bakery springfield il

Post a Comment

Cron Design Studio: Dublin based web design & software development