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

9 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...

jordan shoes
canada goose jackets uk
hermes handbags
kobe shoes
michael kors uk
ralph lauren
buy red bottoms
louis vuitton handbags outlet
rolex replica watches
christian louboutin outlet
20161228caiyan

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

Julie T. Kirwan said...

http://greenfarm3mod.xyz/apk/
http://bullyanniversaryeditionmod.xyz/apk/
https://mxplayerpro.xyz/apk/

cara menggugurkan hamil said...

just information we only provide information for those who need it
A. obat telat datang bulan
B. posisi berhubungan agar cepat hamil
C. makanan dan minuman agar cepat hamil
D. panduan agar cepat hamil
E. cara agar cepat hamil
F. cara agar cepat hamil setelah selesai haid
G. cara alami untuk segera mendapat kehamilan

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

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

Post a Comment

Cron Design Studio: Dublin based web design & software development