(:Summary:String and formatting operations:) (:Audience: authors (advanced) :) The [@{(...)}@] "expression markup" allows for a variety of string and formatting operations to be performed from within markup. Operations defined by this recipe include ''[[#substr|substr]]'', ''[[#ftime|ftime]]'', ''[[#strlen|strlen]]'', ''[[#rand|rand]]'', ''[[#to|toupper / tolower]]'', ''[[#ucfirst|ucfirst]]'', ''[[#ucwords|ucwords]]'', ''[[#pagename|pagename]]'' and ''[[#asspaced|asspaced]]''. [[#substr]] !! ''substr'' The "substr" expression extracts portions of a string. The arguments are # the string to be processed. Always quote the string to be processed. # the initial position of the substring. Note that the initial position argument is zero-based (i.e., the first character is referenced via a "0"). # the number of characters to extract (:markup class="horiz":) {(substr "PmWiki" 2 3)} {(substr "PmWiki" 2)} {(substr "PmWiki" 0 1)} {(substr "PmWiki" 0 -3)} {(substr "PmWiki" -3)} (:markupend:) To obtain the last n characters of a string use [@{(substr "string" -n)}@] [[<<]] To truncate the last n characters of a string use [@(substr "string" 0 -n)}@] [[#ftime]] !! ''ftime'' "Ftime" expressions are used for date and time formatting. The generic form is ->[@{(ftime "fmt" "when")}@] ->[@{(ftime fmt="fmt" when="when")}@] where ''fmt'' is a formatting string and ''when'' is the time to be formatted. The arguments can be in either order and may use the optional "fmt=" and "when=" labels. Examples: (:markup class="horiz":) {(ftime)} {(ftime fmt="%F %H:%M")} {(ftime %Y)} {(ftime fmt=%T)} {(ftime when=tomorrow)} {(ftime fmt="%Y-%m-%d" yesterday)} {(ftime week %F)} {(ftime fmt=%D month)} {(ftime fmt="%a%e %b" when="next week")} (:markupend:) The ''fmt'' parameter is whatever is given by "fmt=", the first parameter containing a '%', or else the site's default. The formatting codes are described at %newwin% http://php.net/strftime. In addition to those, '%F' produces ISO-8601 dates, and '%s' produces Unix timestamps. Some common formatting strings: [= %F =] # ISO-8601 dates "{(ftime %F)}" [= %s =] # Unix timestamp "{(ftime %s)}" [= %H:%M:%S =] # time as hh:mm:ss "{(ftime %H:%M:%S)}" [= %m/%d/%Y =] # date as mm/dd/yyyy "{(ftime %m/%d/%Y)}" [= "%A, %B %d, %Y" =] # in words "{(ftime "%A, %B %d, %Y")}" The ''when'' parameter understands many different date formats. The when parameter is whatever is given by "when=", or whatever parameter remains after determining the format parameter. Some examples: 2007-04-11 # ISO-8601 dates 20070411 # dates without hyphens, slashes, or dots 2007-03 # months @1176304315 # Unix timestamps (seconds since 1-Jan-1970 00:00 UTC) now # the current time today # today @ 00:00:00 yesterday # yesterday @ 00:00:00 "next Monday" # relative dates "last Thursday" # relative dates "-3 days" # three days ago "+2 weeks" # two weeks from now '''Note:''' If you want to convert a Unix timestamp you '''must''' prefix with the @. Thus, [@"{(ftime "%A, %B %d, %Y" @1231116927)}"@]. The ''when'' parameter uses PHP's %newwin%[[(http://www.php.net/)strtotime]] function to convert date strings according to the GNU [[http://www.gnu.org/software/tar/manual/html_node/Date-input-formats.html|date input formats]]; as of this writing it only understands English phrases in date specifications. The variable $FTimeFmt can be used to override the default date format used by the "ftime" function. The default $FTimeFmt is $TimeFmt. [[#strlen]] !! ''strlen'' The "strlen" expression returns the length of a string. The first argument is the string to be measured. (:markup class="horiz":) {(strlen "{$:Summary}")} (:markupend:) [[#rand]] !! ''rand'' The "rand" expression returns a random integer. The first argument is the minimum number to be returned and the second argument is the maximum number to be returned. If called without the optional min, max arguments rand() returns a pseudo-random integer between 0 and RAND_MAX. If you want a random number between 5 and 15 (inclusive), for example, use rand (5, 15). (:markup class="horiz":) {(rand)} (:markupend:) [[#to]] !! ''toupper'' / ''tolower'' The "toupper" and "tolower" expressions convert a string into uppercase or lowercase. The first argument is the string to be processed. (:markup class="horiz":) {(toupper "{$:Summary}")} {(tolower "{$:Summary}")} (:markupend:) [[#ucfirst]] !!''ucfirst'' The "ucfirst" expression converts the first character of a string to uppercase. The first argument is the string to be processed. (:markup class="horiz":) {(ucfirst "{$:Summary}")} (:markupend:) [[#ucwords]] !! ''ucwords'' The "ucwords" expression converts the first character of each word in a string to uppercase. The first argument is the string to be processed. (:markup class="horiz":) {(ucwords "{$:Summary}")} (:markupend:) [[#pagename]] !! ''pagename'' The "pagename" expression builds a pagename from a string. The first argument is the string to be processed. (:markup class="horiz":) {(pagename "{$:Summary}")} (:markupend:) [[#asspaced]] !! ''asspaced'' The "asspaced" expression formats wikiwords. The first argument is the string to be processed. (:markup class="horiz":) {(asspaced "{$FullName}")} (:markupend:) [[#nesting]] !! Nesting expressions Markup expressions can be nested: (:markup class="horiz":) {(tolower (substr "Hello World" 2))} (:markupend:) [[#notes]] !! Notes * Some of the string-processing markups may not work properly on UTF-8 characters or escaped sequences. * The ftime markup does not work with some [[http://mcs.vuw.ac.nz/technical/software/SGML/doc/iso8601/ISO8601.html | ISO 8601]] dates (because a time of 24:00 [[http://gnu.org/software/tar/manual/html_node/tar_114.html | is invalid]])such as: (:markup class=horiz:) {(ftime fmt="%m/%d/%Y @ %H:%M:%S" when="20070626T2400")} {(ftime fmt="%H:%M:%S" when="20070626T2400")} (:markupend:) * is it possible to display the time in another [[http://gnu.org/software/tar/manual/html_node/tar_122.html#SEC122 | time zone]], eg (:markup class=horiz:) NowTime: {(ftime fmt="%F %H:%M")} {(ftime when='TZ=:Europe/London" ')} {(ftime when='TZ=Europe/London" 0 days')} {(ftime when="TZ='Pacific/Auckland' 2004-10-31 08:30")} {(ftime when='TZ="Pacific/Auckland" {$:NowTime}')} (:markupend:) !! See also * [[Page variables]], [[Page text variables]] * [[Conditional markup]] * Cookbook:MarkupExprPlus