Extend a string to a certain length with specific characters

Sometimes it is needed to make a string to a certain length specially for the ticketing software. Let a ticket is going to place by someone to a specific department(let department is support), we need to give him a ticket id something humanize and its length will be always of 10 characters. We want to give him a ticket id something like
SU00000001

Where SU is the department name prefix and 1 is the id of this entry. On that case the php function str_pad is very usefull. just follow the following code which is very simple:

$entry_id=1;
$dept_prefix = 'SU';
$padded_id = str_pad($entry_id,8,'0',STR_PAD_LEFT);
$ticket_id = $dept_prefix.$padded_id;

Love this function!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s