Recent Changes - Search:

Invitation

Please feel free to edit the conversation with answers and solutions. Simply click Edit at the top right of the page and enter teacher as the password. You can insert text, in much the same way that you would with a word processor. However it is a very primitive word processor. You will see, at the bottom of the page, techniques for formatting your messages.

edit SideBar

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, ftime, strlen, rand, toupper / tolower, ucfirst, ucwords, pagename and asspaced.

substr

The "substr" expression extracts portions of a string. The arguments are

  1. the string to be processed. Always quote the string to be processed.
  2. 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").
  3. the number of characters to extract
 {(substr "PmWiki" 2 3)}
 {(substr "PmWiki" 2)}
 {(substr "PmWiki" 0 1)}
 {(substr "PmWiki" 0 -3)}
 {(substr "PmWiki" -3)}
 Wik
 Wiki
 P
 PmW
 iki

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" 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:

 {(ftime)}
 {(ftime fmt="%F %H:%M")}
 {(ftime %Y)}
 {(ftime fmt=%T)}
 {(ftime when=tomorrow)}
 {(ftime fmt="%Y-%m-%d" yesterday)}
 {(ftime "+1 week" %F)}
 {(ftime fmt=%D "+1 month")}
 {(ftime fmt="%a%e %b" when="next
week")}
 March 28, 2024, at 06:35 PM EST
 2024-03-28 18:35
 2024
 18:35:01
 March 29, 2024, at 12:00 AM EST
 2024-03-27
 2024-04-04
 04/28/24
 Mon 1 Apr

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 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      "2024-03-28"
     %s                # Unix timestamp      "1711665301"
     %H:%M:%S          # time as hh:mm:ss    "18:35:01"
     %m/%d/%Y          # date as mm/dd/yyyy  "03/28/2024"
     "%A, %B %d, %Y"   # in words            "Thursday, March 28, 2024"

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 strtotime function to convert date strings according to the GNU 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

The "strlen" expression returns the length of a string. The first argument is the string to be measured.

 {(strlen "{$:Summary}")}
 32

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).

 {(rand)}
 430286081

toupper / tolower

The "toupper" and "tolower" expressions convert a string into uppercase or lowercase. The first argument is the string to be processed.

 {(toupper "{$:Summary}")}
 {(tolower "{$:Summary}")}
 STRING AND FORMATTING OPERATIONS
 string and formatting operations

ucfirst

The "ucfirst" expression converts the first character of a string to uppercase. The first argument is the string to be processed.

 {(ucfirst "{$:Summary}")}
 String and formatting operations

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.

 {(ucwords "{$:Summary}")}
 String And Formatting Operations

pagename

The "pagename" expression builds a pagename from a string. The first argument is the string to be processed.

 {(pagename "{$:Summary}")}
 PmWiki.StringAndFormattingOperations

asspaced

The "asspaced" expression formats wikiwords. The first argument is the string to be processed.

 {(asspaced "{$FullName}")}
 Pm Wiki.Markup Expressions

Nesting expressions

Markup expressions can be nested:

 {(tolower (substr "Hello World" 2))}
 llo world

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 ISO 8601 dates (because a time of 24:00 is invalid)such as:
 {(ftime fmt="%m/%d/%Y @ %H:%M:%S"
when="20070626T2400")}
 {(ftime fmt="%H:%M:%S"
when="20070626T2400")}
 06/27/2007 @ 00:00:00
 00:00:00
  • is it possible to display the time in another time zone, eg
NowTime: {(ftime fmt="%F %H:%M")}
 1 {(ftime when='Europe/London')}
 2 {(ftime when='Europe/London 0 days')}
 3 {(ftime when="Europe/London
2004-10-31 08:00")}
 4 {(ftime when='Pacific/Auckland
{$:NowTime}')} 
->@@#4 doesn't work because of the
order of rules with a markup expression
both setting NowTime (above) and also
using it (in #4)@@

 5 {(ftime when='Europe/London')}
 6 {(ftime when='Europe/London +2
days')}
 7 {(ftime when="Europe/London
20041031T0820")}
 8 {(ftime "%F %H:%M" 'Pacific/Auckland
now')}

NowTime: 2024-03-28 18:35

 1 March 28, 2024, at 02:35 PM EST
 2 March 28, 2024, at 02:35 PM EST
 3 October 31, 2004, at 12:00 AM EST
 4 (ftime when='Pacific/Auckland {2024-03-28 18:35')} 
#4 doesn't work because of the order of rules with a markup expression both setting NowTime (above) and also using it (in #4)
 5 March 28, 2024, at 02:35 PM EST
 6 March 30, 2024, at 02:35 PM EST
 7 October 31, 2004, at 08:20 AM EST
 8 2024-03-28 01:35

See also


This page may have a more recent version on pmwiki.org: PmWiki:MarkupExpressions, and a talk page: PmWiki:MarkupExpressions-Talk.

Edit - History - Print - Recent Changes - Search
Page last modified on April 29, 2011, at 12:53 PM EST