A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.mediawiki.org/wiki/Manual:Performing_string_operations_with_parser_functions below:

Manual:Performing string operations with parser functions

This page was written in 2010, before the development of

Scribunto

. While the techniques described should still work it is generally better to use modules like

Module:String

to perform complex string operations

Various string functions are part of Extension:ParserFunctions. However, this part is disabled on Wikimedia wikis, with $wgPFEnableStringFunctions = false. The following examples show how to perform string operations without using those parser functions.

String concatenation is done by juxtaposition (i.e., simply placing the segments adjacent to one another).

The expansion of the concatenation of two balanced segments of wikitext is equal to the concatenation of the two expanded wikitexts.

However, the expansion of the concatenation of two unbalanced segments, like '''bo and ld''', will not be rendered the same way when those two segments are expanded and then concatenated (bold vs. '''bold''').

Trimming is the removal of newlines and spaces from the start and the end of a string.

Automatically trimmed are:

Thus, if trimming is desired, this is in many cases conveniently performed together with another operation one wants to perform. To just trim string S, one applies a template or parser function such that this does not affect the string, apart from the trimming, e.g.:

For equality there are #ifeq: and #if:. To force a string-based comparison with #ifeq:, add a non-numerical character to both compared arguments.

Note that this prevents the trimming at the side at which the character is added. If desired, trim the arguments first. If trimming is not desired, put a non-numerical character before and after both strings, e.g. put them in quotes.

See Help:Magic words#Formatting.

The following applies to trimmed strings S and P, and n = 0, 1, 2,.. Note that the result is not trimmed: if S is empty the result can end with spaces and/or newlines.

{{padleft:S|n|P}} pads a given string S on the left with a padding string P (default: "0"), to increase the length (where a newline counts as one character) to min(n,500), or if length (S) ≥ min(n,500) it just returns S. The padding string P is zero or more times repeated, with finally possibly a truncated P. If P is empty, S is returned.

Properties:

An important special case is that where S is empty: {{padleft:|n|P}} produces a string of length min(n,500) consisting of zero or more repetitions of P, with finally possibly a truncated P. In particular, if P has a length greater than min(n,500), this is the truncation of P to min(n,500) characters, i.e., the substring of P of min(n,500) characters starting at the start of P.

The above-mentioned properties reduce to:

Examples:

Maximum string length[edit]

If characters of P are in the result (P is not empty and the string is not already longer than the required length and not longer than 500) the maximum length of the resulting string is 500:

However, if no characters of P are in the result (P is empty or the string is already longer than the required length or longer than 500), the whole string S is returned, even if it is longer:

Applications with respect to a page[edit]

Rendering the truncated expanded wikitext of a page:

{{padleft:|500|{{Help:Parser function}}}} gives " <table class="template-pd-help-page"><tr> <td class="icon-cell">[[File:PD-icon.svg|30px|link=|alt=PD]]</td> <td>'''Note:''' When you edit this page, you agree to release your contribution under the [https://creativecommons.org/publicdomain/zero/1.0/ CC0]. See [[Special:MyLanguage/Project:PD help|Public Domain Help Pages]] for more info. </td> <td class="icon-cell">[[File:PD-icon.svg|30px|link=|alt=PD]]</td> </tr></table> <div style="clear: right; margin-bottom: .5em; float: right; margin-left:"

Note that when specifying an arbitrary number of characters. links, table structure, XML-style tags etc. may be broken. However, if the braces are balanced in the source page there are neither unbalanced braces in the result, because expansion is done before truncation.

Displaying the truncated expanded wikitext:

{{#tag:nowiki|{{padleft:|500|{{Help:Parser function}}}}}} gives " <table class="template-pd-help-page"><tr> <td class="icon-cell">[[File:PD-icon.svg|30px|link=|alt=PD]]</td> <td>'''Note:''' When you edit this page, you agree to release your contribution under the [https://creativecommons.org/publicdomain/zero/1.0/ CC0]. See [[Special:MyLanguage/Project:PD help|Public Domain Help Pages]] for more info. </td> <td class="icon-cell">[[File:PD-icon.svg|30px|link=|alt=PD]]</td> </tr></table> <div style="clear: right; margin-bottom: .5em; float: right; margin-left:"

Displaying the truncated wikitext:

{{padleft:|500|{{msgnw:Help:Parser function}}}} gives "<languages /> {{PD Help Page}} {{TOCRight}} '''Magic words''' are strings of text that MediaWiki associates with a return value or function, such as time, site details, or page names. This page explains only the standard magic words; for a technical reference, see {{ll|Manual:Magic words}}. There are three general types of magic words: * '''[[#Behavior switches|"

{{padleft:|500|{{Help:Template}}}} gives "{{padleft:|500|{{Help:Template}}}}" (bugged display, example suppressed)

The page has been prepared with <onlyinclude> tags, so that the small maximum of 500 characters is not wasted on a header box or table code. This also reduces the size of what is transcluded before applying padleft, reducing the post-expand include size, for which there is a limit of 2048000 bytes per page. A disadvantage could be that other pages cannot include the rest of the page either.

The following applies to trimmed strings S and P, and n = 0, 1, 2,.. Note that the result is not trimmed.

{{padright:S|n|P}} pads a given string S on the right with a padding string P (default: "0"), to increase the length to n, or if length (S) ≥ n it just returns S. The padding string P is zero or more times repeated, with finally possibly a truncated P. If P is empty, S is returned.

Properties:

The special case where S is empty is the same as for padleft.

Examples:

Length of a string[edit]

There is no function for the length of a string, but from the above follows:

(Note that {{padleft:S|n}} may end with spaces and/or newlines, so care should be taken that it is not trimmed before the comparison.)

Thus with a binary search the length can be found, except that if it is 500 or more this fact can be found, not the actual length. This is done in m:Template:Len with the help of m:Template:Len/digit. They use quotation marks around the string, so the maximum length found for the string itself is 498.

As follows from the above, the first character of a string P can be extracted with {{padleft:|1|P}}. This method is preferable for this case compared with the method below, for efficiency and because there are less limitations.

There is no function for extracting a character from an arbitrary given position of a string. However, for a given character we can compare the truncation of the string up to and including the given position with the truncation of the string until that position, concatenated with the character. Thus we determine whether the character at the given position in the string is equal to the character we tried. (Note that the truncation of the string until the given position may end with spaces and/or newlines, so care should be taken it is not trimmed before the concatenation. Similarly, when trying whether the character is a space or newline, care should be taken that the compared strings are not trimmed before the comparison, because then we would not distinguish between a space and a newline.) Also care should be taken that the result, which may be a space or a newline, is not unintentionally trimmed.

This is done in m:Template:Chr with the help of m:Template:Chr/list. The latter contains a switch with a case for each of the supported characters.

The automatic newline feature/bug for "{|" does not affect the result, except that it adds the newline if "{|" is at the start of the substring, since Sub calls Chr for every character position separately. However, just "*", "#", ":", and ";" without newline cannot be produced by any template or parser function. Possible remedies:

As mentioned above, if n is not greater than the length of a string P, {{padleft:|n|P}} produces the truncation of P to n characters, i.e., the substring of P of n characters starting at the start of P. This method is preferable for this case compared with the method below, for efficiency and because there are less limitations.

There is no function for extracting a substring that does not start at the start of the string, neither one for looping. However, for every potential position we can determine whether it is within the range of the required substring, and if so, extract the character. The results are simply concatenated.

For each of the required character positions it requires two calls of padleft and a call of a switch. Thus quickly system limits are reached.

Extracting a substring from some given position to the end of a string requires the specification or determination of the length of the string. This also applies to extracting a substring of a given length from the end of the string. Of course, if the length is known it is more efficient to specify it than to have the system determine it.

XML-style tags, e.g. <nowiki> tags and <math> tags, together with their content, are temporarily replaced by a so-called strip marker, a unique code with a length of ca. 37 characters plus the length of the tag name (independent of the length of the content), e.g.

[1]

This affects string functions. If a strip marker is truncated, the remainder is exposed. Also, padding a string with a strip marker is based on the length of that, not of what it represents.

Examples with <nowiki> tags:

Examples with <math> tags:

Suppose we have the string ABCD and also need the substring CD, then because of the inefficiency of finding the substring we can better store (or pass on as parameter values) AB and CD as separate data items.

Date and time variables are available providing output in the form of numbers, allowing easy processing without expensive string operations.

However, for processing the result of {{PAGENAME}} (on the page itself or passed on to a template as parameter value) string functions can be useful, to extract the text after "Wikiproject ", the text between parentheses, the text before the parentheses, etc.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.3