version=pmwiki-2.2.5 ordered=1 urlencoded=1 agent=Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.8) Gecko/2009033100 Ubuntu/9.04 (jaunty) Firefox/3.0.8 author=Rafa charset=ISO-8859-1 csum= ctime=1176718061 host=194.51.102.250 name=PmWiki.MarkupExpressions rev=60 targets=PmWiki.PageVariables,PmWiki.PageTextVariables,PmWiki.ConditionalMarkup text=(:Summary:String and formatting operations:)%0a(:Audience: authors (advanced) :)%0a%0aThe [@{(...)}@] "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]]''.%0a%0a[[#substr]]%0a!! ''substr''%0a%0aThe "substr" expression extracts portions of a string. The arguments are%0a# the string to be processed. Always quote the string to be processed.%0a# 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").%0a# the number of characters to extract%0a(:markup class="horiz":)%0a {(substr "PmWiki" 2 3)}%0a {(substr "PmWiki" 2)}%0a {(substr "PmWiki" 0 1)}%0a {(substr "PmWiki" 0 -3)}%0a {(substr "PmWiki" -3)}%0a(:markupend:)%0a%0aTo obtain the last n characters of a string use [@{(substr "string" -n)}@]%0a[[%3c%3c]]%0aTo truncate the last n characters of a string use [@(substr "string" 0 -n)}@]%0a%0a[[#ftime]]%0a!! ''ftime''%0a%0a"Ftime" expressions are used for date and time formatting. The generic form is%0a%0a->[@{(ftime "fmt" "when")}@]%0a->[@{(ftime fmt="fmt" when="when")}@]%0a%0awhere ''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.%0a%0aExamples:%0a(:markup class="horiz":)%0a {(ftime)}%0a {(ftime fmt="%25F %25H:%25M")}%0a {(ftime %25Y)}%0a {(ftime fmt=%25T)}%0a {(ftime when=tomorrow)}%0a {(ftime fmt="%25Y-%25m-%25d" yesterday)}%0a {(ftime week %25F)}%0a {(ftime fmt=%25D month)}%0a {(ftime fmt="%25a%25e %25b" when="next week")}%0a(:markupend:)%0a%0aThe ''fmt'' parameter is whatever is given by "fmt=", the first parameter containing a '%25', or else the site's default. The formatting codes are described at %25newwin%25 http://php.net/strftime. In addition to those, '%25F' produces ISO-8601 dates, and '%25s' produces Unix timestamps.%0aSome common formatting strings:%0a%0a [= %25F =] # ISO-8601 dates "{(ftime %25F)}"%0a [= %25s =] # Unix timestamp "{(ftime %25s)}"%0a [= %25H:%25M:%25S =] # time as hh:mm:ss "{(ftime %25H:%25M:%25S)}"%0a [= %25m/%25d/%25Y =] # date as mm/dd/yyyy "{(ftime %25m/%25d/%25Y)}"%0a [= "%25A, %25B %25d, %25Y" =] # in words "{(ftime "%25A, %25B %25d, %25Y")}"%0a%0aThe ''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:%0a%0a 2007-04-11 # ISO-8601 dates%0a 20070411 # dates without hyphens, slashes, or dots%0a 2007-03 # months%0a @1176304315 # Unix timestamps (seconds since 1-Jan-1970 00:00 UTC)%0a now # the current time%0a today # today @ 00:00:00%0a yesterday # yesterday @ 00:00:00%0a "next Monday" # relative dates%0a "last Thursday" # relative dates%0a "-3 days" # three days ago%0a "+2 weeks" # two weeks from now%0a%0a'''Note:''' If you want to convert a Unix timestamp you '''must''' prefix with the @. Thus, [@"{(ftime "%25A, %25B %25d, %25Y" @1231116927)}"@].%0a%0aThe ''when'' parameter uses PHP's %25newwin%25[[(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.%0a%0aThe variable $FTimeFmt can be used to override the default date format used by the "ftime" function. The default $FTimeFmt is $TimeFmt.%0a%0a[[#strlen]]%0a!! ''strlen''%0a%0aThe "strlen" expression returns the length of a string. The first argument is the string to be measured.%0a%0a(:markup class="horiz":)%0a {(strlen "{$:Summary}")}%0a(:markupend:)%0a%0a[[#rand]]%0a!! ''rand''%0a%0aThe "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).%0a%0a(:markup class="horiz":)%0a {(rand)}%0a(:markupend:)%0a%0a[[#to]]%0a!! ''toupper'' / ''tolower''%0a%0aThe "toupper" and "tolower" expressions convert a string into uppercase or lowercase. The first argument is the string to be processed.%0a%0a(:markup class="horiz":)%0a {(toupper "{$:Summary}")}%0a {(tolower "{$:Summary}")}%0a(:markupend:)%0a%0a[[#ucfirst]]%0a!!''ucfirst''%0a%0aThe "ucfirst" expression converts the first character of a string to uppercase. The first argument is the string to be processed.%0a%0a(:markup class="horiz":)%0a {(ucfirst "{$:Summary}")}%0a(:markupend:)%0a%0a[[#ucwords]]%0a!! ''ucwords''%0a%0aThe "ucwords" expression converts the first character of each word in a string to uppercase. The first argument is the string to be processed.%0a%0a(:markup class="horiz":)%0a {(ucwords "{$:Summary}")}%0a(:markupend:)%0a%0a[[#pagename]]%0a!! ''pagename''%0a%0aThe "pagename" expression builds a pagename from a string. The first argument is the string to be processed.%0a%0a(:markup class="horiz":)%0a {(pagename "{$:Summary}")}%0a(:markupend:)%0a%0a[[#asspaced]]%0a!! ''asspaced''%0a%0aThe "asspaced" expression formats wikiwords. The first argument is the string to be processed.%0a%0a(:markup class="horiz":)%0a {(asspaced "{$FullName}")}%0a(:markupend:)%0a%0a[[#nesting]]%0a!! Nesting expressions%0a%0aMarkup expressions can be nested:%0a%0a(:markup class="horiz":)%0a {(tolower (substr "Hello World" 2))}%0a(:markupend:)%0a%0a[[#notes]] %0a!! Notes%0a%0a* Some of the string-processing markups may not work properly on UTF-8 characters or escaped sequences.%0a* 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: %0a(:markup class=horiz:)%0a {(ftime fmt="%25m/%25d/%25Y @ %25H:%25M:%25S" when="20070626T2400")}%0a {(ftime fmt="%25H:%25M:%25S" when="20070626T2400")}%0a(:markupend:)%0a* is it possible to display the time in another [[http://gnu.org/software/tar/manual/html_node/tar_122.html#SEC122 | time zone]], eg%0a(:markup class=horiz:)%0aNowTime: {(ftime fmt="%25F %25H:%25M")}%0a {(ftime when='TZ=:Europe/London" ')}%0a {(ftime when='TZ=Europe/London" 0 days')}%0a {(ftime when="TZ='Europe/London' 2004-10-31 08:00")}%0a {(ftime when='TZ="Pacific/Auckland" {$:NowTime}')}%0a(:markupend:)%0a%0a!! See also%0a* [[Page variables]], [[Page text variables]]%0a* [[Conditional markup]]%0a* Cookbook:MarkupExpressionSamples — '-custom markup expression samples-'%0a* Cookbook:MarkupExprPlus%0a time=1254220873