Quick and dirty regex question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
eazyGen
Forum Commoner
Posts: 46
Joined: Mon Aug 29, 2011 4:32 am
Location: Central London

Quick and dirty regex question

Post by eazyGen »

Hi guys,

I would normally research this myself, but I am in a hurry just now.

My question is - how would I take a string like:

"Customer Name"

or

"Product Cost Price"

and produce a result where all upper case letters are made lower case and all spaces replaced by under-scores. Like this:

"Customer Name" would become "customer_name", and

"Product Cost Price" would become "product_cost_price"

Many thanks for any swift help,

S
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Quick and dirty regex question

Post by Celauran »

Not regex, but this should do the job:

Code: Select all

strtolower(str_replace(' ', '_', $string))
User avatar
eazyGen
Forum Commoner
Posts: 46
Joined: Mon Aug 29, 2011 4:32 am
Location: Central London

Re: Quick and dirty regex question

Post by eazyGen »

Celauran wrote:Not regex, but this should do the job:

Code: Select all

strtolower(str_replace(' ', '_', $string))
Thank you.

Sorry to appear lazy, but I was in a mad rush.

S
Post Reply