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

12 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

Unknown said...

20180623xiaoke
polo ralph lauren outlet
air max 2018
cheap jordans
canada goose jackets
coach outlet store online
dsquared2 jeans
cheap jordan shoes
oakley sunglasses wholesale
michael kors outlet clearance
coach outlet store online

محمد على 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.

Unknown said...

The development of artificial intelligence (AI) has propelled more programming architects, information scientists, and different experts to investigate the plausibility of a vocation in machine learning. Notwithstanding, a few newcomers will in general spotlight a lot on hypothesis and insufficient on commonsense application. machine learning projects for final year In case you will succeed, you have to begin building machine learning projects in the near future.

Projects assist you with improving your applied ML skills rapidly while allowing you to investigate an intriguing point. Furthermore, you can include projects into your portfolio, making it simpler to get a vocation, discover cool profession openings, and Final Year Project Centers in Chennai even arrange a more significant compensation.

Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account.

The Nodejs Projects Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training

مجدى 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

Post a Comment

Cron Design Studio: Dublin based web design & software development