|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
David Smarty Rookie
Joined: 18 Apr 2003 Posts: 17 Location: New York, NY, USA
|
Posted: Tue May 06, 2003 2:44 pm Post subject: here's a 10-digit phone number formatting plugin |
|
|
ok, it's pretty trivial, I grant you, but this being my first smarty modifier plugin, i was too excited to keep it to myself.
default format is (xxx) xxx-xxxx but you can pass it whatever sprintf-style format string you like.
[php:1:57377f565f]
<?php
/*
* Smarty plugin
* -------------------------------------------------------------
* File: modifier.format_phone.php
* Type: modifier
* Name: format_phone
* Purpose: format a 10-digit phone number
* -------------------------------------------------------------
*/
function smarty_modifier_format_phone($number, $format="(%s) %s-%s") {
$number = preg_replace("/\D/","",$number);
if (strlen($number) != 10) return $number;
$number = sprintf(
$format,
substr($number,0,3),
substr($number,3,3),
substr($number,6,4)
);
return $number;
}
?>
[/php:1:57377f565f]
usage:
Code: |
Custom phone formatter: {"2128050362"|format_phone:'%s/%s.%s'} |
_________________ David Mintz
http://davidmintz.org/
"Anybody else got a problem with Webistics?" -- Sopranos 24:17 |
|
Back to top |
|
ZeroFill Smarty Rookie
Joined: 31 Aug 2003 Posts: 28
|
Posted: Sat Dec 20, 2003 6:41 pm Post subject: |
|
|
Awesome! Thank you david, this is something i was looking for _________________ my delimiters are <{ and }> |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Mon Dec 22, 2003 3:54 pm Post subject: |
|
|
A couple of suggestions:
In your logic, if the phone number isn't exactly 10 digits you return the stripped down value. It would be better to return the original value untouched.
Call the modifier "phone_format" instead of "format_phone" to coincide with the naming convention of "date_format"
It would be useful to accomodate phone numbers of other formats, such as international numbers, or if there was a "1" prepended to the number. I'll bet there are all kinds of phone number formatters out there you could look at to be sure the logic is complete and accurate.
Thanks for your contribution to smarty! |
|
Back to top |
|
David Smarty Rookie
Joined: 18 Apr 2003 Posts: 17 Location: New York, NY, USA
|
Posted: Mon Dec 22, 2003 9:23 pm Post subject: |
|
|
woa -- didn''t Zerofill just get done telling you my plugin was awesome?
kidding aside, the points are very well taken -- I'll post a revised version here just as soon as I get off my derrière and do it. _________________ David Mintz
http://davidmintz.org/
"Anybody else got a problem with Webistics?" -- Sopranos 24:17 |
|
Back to top |
|
David Smarty Rookie
Joined: 18 Apr 2003 Posts: 17 Location: New York, NY, USA
|
Posted: Fri Mar 12, 2004 5:03 pm Post subject: |
|
|
Code: |
/**
* @desc<pre>Smarty plugin
* -------------------------------------------------------------
* File: modifier.phone_format.php
* Type: modifier
* Name: format_phone
* Purpose: format a US-style 10-digit phone number
* -------------------------------------------------------------
*</pre>
* @param string $number a phone number
* @param $format a printf style format string
* @return string $number, the number formatted if 10 digits, else original untouched.
* @author dmintz@davidmintz.org
*/
function smarty_modifier_phone_format($number, $format="(%s) %s-%s") {
$original = $number;
$number = preg_replace("/\D/","",$number);
if (strlen($number) != 10) return $original;
return sprintf(
$format,
substr($number,0,3),
substr($number,3,3),
substr($number,6,4)
);
}
|
----
Changes:
* Change the name to conform to Smarty convention
* Added phpdoc style comments
* Added a wee itty bitty performance optimization (it now returns formatted string straight away, instead of saving it in $number and then returning it)
Enjoy! _________________ David Mintz
http://davidmintz.org/
"Anybody else got a problem with Webistics?" -- Sopranos 24:17 |
|
Back to top |
|
jausions Smarty Rookie
Joined: 06 Nov 2003 Posts: 10
|
Posted: Sat Mar 20, 2004 4:39 am Post subject: Check out the existing phone_number() modifier plugin |
|
|
Hi David,
Just to let you know that I've already written such a modifier: please see thread: http://www.phpinsider.com/smarty-forum/viewtopic.php?t=1364
The modifier supports several formatting options, multiple countries, toll-number (1-800, ...), extension information (i.e. Ext. # 123). The modifier attempts to clean the number from garbage characters such as dashes, dots and so on before formatting... (usefull when data collected have not been standardized.)
Modifier page is located here:
http://smarty.incutio.com/?page=PhoneFormatPlugin
-Philippe
http://www.11abacus.com/ |
|
Back to top |
|
Bongitup Smarty n00b
Joined: 06 May 2010 Posts: 2
|
Posted: Sat Jun 26, 2010 8:36 pm Post subject: |
|
|
So if i were to use this phone plug in you posted how would i go about building the tpl page. Would i need separate textarea's for each part of the phone number? And which variables would i use with each? |
|
Back to top |
|
jundette21 Smarty n00b
Joined: 18 Aug 2010 Posts: 1
|
Posted: Thu Aug 19, 2010 1:49 am Post subject: |
|
|
Well, that was a good suggestion Mohrt. Just to make it sure the logic is accurate and complete in favor of different formatters of phone numbers. Thanks for that valuable information Mohrt. |
|
Back to top |
|
Jagarm Smarty Rookie
Joined: 20 Mar 2009 Posts: 28
|
Posted: Mon Jan 11, 2016 3:18 am Post subject: Great plugin |
|
|
Thank you for sharing this great modifier, I just happen to be looking for one and found it.
Keep up |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|
|