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

11 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

Post a Comment

Cron Design Studio: Dublin based web design & software development