PHP Class Gdn_Format, vanilla

Utility class that helps to format strings, objects, and arrays.
显示文件 Open project: vanilla/vanilla Class Usage Examples

Public Properties

Property Type Description
$DisplayNoFollow Flag which allows plugins to decide if the output should include rel="nofollow" on any links.
$FormatLinks Whether or not to replace plain text links with anchors.
$MentionsUrlFormat string

Protected Properties

Property Type Description
$SanitizedFormats array
$_CleanChars Unicode to ascii conversion table.
$_UrlTranslations array

Public Methods

Method Description
activityHeadline ( object $Activity, integer $ProfileUserID = '', $ViewingUserID = '' ) : string The ActivityType table has some special sprintf search/replace values in the FullHeadline and ProfileHeadline fields.
alphaNumeric ( string $Mixed ) : string Removes all non-alpha-numeric characters (except for _ and -) from
arrayAsAttributes ( array $Array ) : string
arrayAsObject ( array $Array ) : stdClass Takes an object and convert's it's properties => values to an associative array of $Array[Property] => Value sets.
arrayValueForPhp ( $String ) : string Formats a string so that it can be saved to a PHP file in double-quotes of an array value assignment.
auto ( mixed $Mixed ) : string Takes a mixed variable, filters unsafe things, renders BBCode and returns it.
bbCode ( mixed $Mixed ) : string Takes a mixed variable.
bigNumber ( mixed $Number, $Format = '' ) : string Format a number by putting K/M/B suffix after it when appropriate.
bytes ( integer $Bytes, integer $Precision = 2 ) : string Format a number as if it's a number of bytes by adding the appropriate B/K/M/G/T suffix.
clean ( mixed $Mixed ) : string Convert certain unicode characters into their ascii equivalents.
date ( string $Timestamp = '', string $Format = '' ) : string Formats a Mysql DateTime string in the specified format.
dateFull ( integer $Timestamp, string $Format = '' ) Formats a MySql datetime or a unix timestamp for display in the system.
defaultFormat ( boolean | null $is_mobile = null ) : string Return the default input formatter.
deleted ( mixed $Mixed ) : string Format a string from of "Deleted" content (comment, message, etc).
display ( mixed $Mixed ) : string Takes a mixed variable, formats it for display on the screen, and returns it.
email ( string $Email ) : string Formats an email address in a non-scrapable format.
form ( mixed $Mixed ) : string Takes a mixed variable, formats it for display in a form, and returns it.
fuzzyTime ( $Timestamp = null, $MorePrecise = false ) : string Show times relative to now, e.g. "4 hours ago".
getEmbedSize ( ) : array Returns embedded video width and height, based on configuration.
html ( mixed $Mixed ) : string Takes a mixed variable, filters unsafe HTML and returns it.
htmlFilter ( mixed $Mixed ) : string Takes a mixed variable, filters unsafe HTML and returns it.
image ( string $Body ) : string Format an encoded string of image properties as HTML.
links ( mixed $Mixed ) : string Formats the anchor tags around the links in text.
markdown ( mixed $Mixed, boolean $Flavored = true ) : string Format a string using Markdown syntax. Also purifies the output HTML.
mentions ( $Mixed ) : mixed | string Handle mentions formatting.
objectAsArray ( object $Object ) : unknown Takes an object and converts its properties => values to an associative array of $Array[Property] => Value sets.
plainText ( string $Body, string $Format = 'Html' ) : string Format a string as plain text.
raw ( string | object | array $Mixed ) : string Return the input without any operations performed at all.
reduceWhiteSpaces ( string $String ) Reduces multiple whitespaces including line breaks and tabs to one single space character.
replaceButProtectCodeBlocks ( mixed $Search, mixed $Replace, mixed $Subject, boolean $IsCallback = false ) : string Do a preg_replace, but don't affect things inside tags.
rssHtml ( string $Text, string $Format = 'Html' ) : string Format some text in a way suitable for passing into an rss/atom feed.
seconds ( integer $Seconds ) : string Formats seconds in a human-readable way (ie. 45 seconds, 15 minutes, 2 hours, 4 days, 2 months, etc).
serialize ( mixed $Mixed ) : string Takes any variable and serializes it.
spoilerHtml ( string $spoilerText ) : string Returns spoiler text wrapped in a HTML spoiler wrapper.
tagContent ( string $html, callable $callback, boolean $skipAnchors = true, boolean $skipCode = true ) : string Executes the callback function on parts of the string excluding html tags.
text ( mixed $mixed, $addBreaks = true ) : string Takes a mixed variable, formats it for display on the screen as plain text.
textEx ( string $Str ) : string Process as plain text + our magic formatting.
timespan ( integer $timespan ) : string
to ( mixed $Mixed, string $FormatMethod ) : mixed Takes a mixed variable, formats it in the specified format type, and returns it.
toDate ( integer $Timestamp = '' ) : string Format a timestamp or the current time to go into the database.
toDateTime ( integer $Timestamp = '' ) : string Format a timestamp or the current time to go into the database.
toTimestamp ( string $DateTime = '' ) : string | boolean Convert a datetime to a timestamp.
toTimezone ( integer $Timestamp ) : integer Formats a timestamp to the current user's timezone.
unembedContent ( mixed $Mixed ) : HTML Strips out embed/iframes we support and replaces with placeholder.
unserialize ( string $SerializedString ) : mixed Takes a serialized variable and unserializes it back into its original state.
url ( mixed $Mixed ) : string Creates URL codes containing only lowercase Roman letters, digits, and hyphens.
vanillaSprintf ( $PlaceholderString, $ReplaceWith ) : mixed
wysiwyg ( $Mixed ) : mixed | string Format text from WYSIWYG editor input.

Protected Methods

Method Description
formatMentionsCallback ( string $str ) : string Adds a link to all mentions in a given string.
isHtml ( $Text ) : boolean Detect HTML for the purposes of doing advanced filtering.
legacySpoilers ( string $html ) : string Spoilers with backwards compatibility.
linksCallback ( array $Matches ) : string Transform match to clickable links or to embedded equivalent.
listCallback ( array $Matches ) : string Formats BBCode list items.
processHTML ( $html ) : string Performs replacing operations on a HTML string. Usually for formatting posts.
replaceListItems ( string $html ) : string Replaces opening html list tags with an asterisk and closing list tags with new lines.
replaceSpoilers ( string $html, string $replaceWith = '(Spoiler)' ) : string Check to see if a string has spoilers and replace them with an innocuous string.
vanillaSprintfCallback ( $Match, boolean $InternalReplacementList = false ) : mixed

Method Details

activityHeadline() public static method

The ProfileHeadline field is to be used on this page (the user profile page). The FullHeadline field is to be used on the main activity page. The replacement definitions are as follows: %1$s = ActivityName %2$s = ActivityName Possessive %3$s = RegardingName %4$s = RegardingName Possessive %5$s = Link to RegardingName's Wall %6$s = his/her %7$s = he/she %8$s = route & routecode %9$s = gender suffix (some languages require this).
public static activityHeadline ( object $Activity, integer $ProfileUserID = '', $ViewingUserID = '' ) : string
$Activity object An object representation of the activity being formatted.
$ProfileUserID integer If looking at a user profile, this is the UserID of the profile we are looking at.
return string

alphaNumeric() public static method

Removes all non-alpha-numeric characters (except for _ and -) from
public static alphaNumeric ( string $Mixed ) : string
$Mixed string An object, array, or string to be formatted.
return string

arrayAsAttributes() public static method

Deprecation: 9 Nov 2016
public static arrayAsAttributes ( array $Array ) : string
$Array array
return string

arrayAsObject() public static method

Takes an object and convert's it's properties => values to an associative array of $Array[Property] => Value sets.
public static arrayAsObject ( array $Array ) : stdClass
$Array array An array to be converted to object.
return stdClass

arrayValueForPhp() public static method

Formats a string so that it can be saved to a PHP file in double-quotes of an array value assignment.
public static arrayValueForPhp ( $String ) : string
return string

auto() public static method

Takes a mixed variable, filters unsafe things, renders BBCode and returns it.
public static auto ( mixed $Mixed ) : string
$Mixed mixed An object, array, or string to be formatted.
return string

bbCode() public static method

Takes a mixed variable.
public static bbCode ( mixed $Mixed ) : string
$Mixed mixed An object, array, or string to be formatted.
return string

bigNumber() public static method

Format a number by putting K/M/B suffix after it when appropriate.
public static bigNumber ( mixed $Number, $Format = '' ) : string
$Number mixed The number to format. If a number isn't passed then it is returned as is.
return string The formatted number.

bytes() public static method

Format a number as if it's a number of bytes by adding the appropriate B/K/M/G/T suffix.
public static bytes ( integer $Bytes, integer $Precision = 2 ) : string
$Bytes integer The bytes to format.
$Precision integer The number of decimal places to return.
return string The formatted bytes.

clean() public static method

Convert certain unicode characters into their ascii equivalents.
public static clean ( mixed $Mixed ) : string
$Mixed mixed The text to clean.
return string

date() public static method

For instructions on how the format string works:
public static date ( string $Timestamp = '', string $Format = '' ) : string
$Timestamp string A timestamp or string in Mysql DateTime format. ie. YYYY-MM-DD HH:MM:SS
$Format string The format string to use. Defaults to the application's default format.
return string

dateFull() public static method

Formats a MySql datetime or a unix timestamp for display in the system.
Since: 2.1
public static dateFull ( integer $Timestamp, string $Format = '' )
$Timestamp integer
$Format string

defaultFormat() public static method

Return the default input formatter.
public static defaultFormat ( boolean | null $is_mobile = null ) : string
$is_mobile boolean | null Whether or not you want the format for mobile browsers.
return string

deleted() public static method

Format a string from of "Deleted" content (comment, message, etc).
public static deleted ( mixed $Mixed ) : string
$Mixed mixed An object, array, or string to be formatted.
return string

display() public static method

Takes a mixed variable, formats it for display on the screen, and returns it.
public static display ( mixed $Mixed ) : string
$Mixed mixed An object, array, or string to be formatted.
return string

email() public static method

Formats an email address in a non-scrapable format.
public static email ( string $Email ) : string
$Email string
return string

form() public static method

Takes a mixed variable, formats it for display in a form, and returns it.
public static form ( mixed $Mixed ) : string
$Mixed mixed An object, array, or string to be formatted.
return string

formatMentionsCallback() protected static method

Supports most usernames by using double-quotes, for example: @"a $pecial user's! name." Without double-quotes, a mentioned username is terminated by any of the following characters: whitespace | . | , | ; | ? | ! | : | '
Since: 2.3
protected static formatMentionsCallback ( string $str ) : string
$str string The html-formatted string to format mentions in.
return string The formatted string.

fuzzyTime() public static method

Credit goes to: http://byteinn.com/res/426/Fuzzy_Time_function/
public static fuzzyTime ( $Timestamp = null, $MorePrecise = false ) : string
return string

getEmbedSize() public static method

Returns embedded video width and height, based on configuration.
public static getEmbedSize ( ) : array
return array array(Width, Height)

html() public static method

Does "magic" formatting of links, mentions, link embeds, emoji, & linebreaks.
public static html ( mixed $Mixed ) : string
$Mixed mixed An object, array, or string to be formatted.
return string HTML

htmlFilter() public static method

Use this instead of Gdn_Format::Html() when you do not want magic formatting.
public static htmlFilter ( mixed $Mixed ) : string
$Mixed mixed An object, array, or string to be formatted.
return string HTML

image() public static method

Format an encoded string of image properties as HTML.
public static image ( string $Body ) : string
$Body string a encoded array of image properties (Image, Thumbnail, Caption)
return string HTML

isHtml() protected static method

Detect HTML for the purposes of doing advanced filtering.
protected static isHtml ( $Text ) : boolean
$Text
return boolean

legacySpoilers() protected static method

In the Spoilers plugin, we would render BBCode-style spoilers in any format post and allow a title.
protected static legacySpoilers ( string $html ) : string
$html string
return string

linksCallback() protected static method

URLs are typically matched against, which are then translated into a clickable link or transformed into their equivalent embed, if supported. There is a universal config to disable automatic embedding.
protected static linksCallback ( array $Matches ) : string
$Matches array Captured and grouped matches against string.
return string

listCallback() protected static method

Formats BBCode list items.
protected static listCallback ( array $Matches ) : string
$Matches array
return string

markdown() public static method

Format a string using Markdown syntax. Also purifies the output HTML.
public static markdown ( mixed $Mixed, boolean $Flavored = true ) : string
$Mixed mixed An object, array, or string to be formatted.
$Flavored boolean Optional. Parse with Vanilla-flavored settings? Default true
return string

mentions() public static method

Handle mentions formatting.
public static mentions ( $Mixed ) : mixed | string
$Mixed
return mixed | string

objectAsArray() public static method

Takes an object and converts its properties => values to an associative array of $Array[Property] => Value sets.
public static objectAsArray ( object $Object ) : unknown
$Object object The object to be converted to an array.
return unknown

plainText() public static method

Format a string as plain text.
Since: 2.1
public static plainText ( string $Body, string $Format = 'Html' ) : string
$Body string The text to format.
$Format string The current format of the text.
return string

processHTML() protected static method

Runs an HTML string through the links, mentions, emoji and spoilers formatters.
protected static processHTML ( $html ) : string
$html An unparsed HTML string.
return string The formatted HTML string.

raw() public static method

This format should only be used when administrators have access.
Deprecation: 9 Nov 2016
public static raw ( string | object | array $Mixed ) : string
$Mixed string | object | array The data to format.
return string

reduceWhiteSpaces() public static method

Reduces multiple whitespaces including line breaks and tabs to one single space character.
public static reduceWhiteSpaces ( string $String )
$String string The string which should be optimized

replaceButProtectCodeBlocks() public static method

The three parameters are identical to the ones you'd pass preg_replace.
public static replaceButProtectCodeBlocks ( mixed $Search, mixed $Replace, mixed $Subject, boolean $IsCallback = false ) : string
$Search mixed The value being searched for, just like in preg_replace or preg_replace_callback.
$Replace mixed The replacement value, just like in preg_replace or preg_replace_callback.
$Subject mixed The string being searched.
$IsCallback boolean If true, do preg_replace_callback. Do preg_replace otherwise.
return string

replaceListItems() protected static method

Accepts both encoded and decoded html strings.
protected static replaceListItems ( string $html ) : string
$html string An HTML-formatted string.
return string Returns the html with all list items removed.

replaceSpoilers() protected static method

Good for displaying excerpts from discussions and without showing the spoiler text.
protected static replaceSpoilers ( string $html, string $replaceWith = '(Spoiler)' ) : string
$html string An HTML-formatted string.
$replaceWith string The translation code to replace spoilers with.
return string Returns the html with spoilers removed.

rssHtml() public static method

Format some text in a way suitable for passing into an rss/atom feed.
Since: 2.1
public static rssHtml ( string $Text, string $Format = 'Html' ) : string
$Text string The text to format.
$Format string The current format of the text.
return string

seconds() public static method

Formats seconds in a human-readable way (ie. 45 seconds, 15 minutes, 2 hours, 4 days, 2 months, etc).
public static seconds ( integer $Seconds ) : string
$Seconds integer
return string

serialize() public static method

Takes any variable and serializes it.
public static serialize ( mixed $Mixed ) : string
$Mixed mixed An object, array, or string to be serialized.
return string The serialized version of the string.

spoilerHtml() public static method

Parsers for NBBC and Markdown should use this function to format thier spoilers. All spoilers in HTML-formatted posts are saved in this way. We use javascript in spoilers.js to add markup and render Spoilers with the "Spoiler" css class name.
public static spoilerHtml ( string $spoilerText ) : string
$spoilerText string The inner text of the spoiler.
return string
Takes a mixed variable, formats it for display on the screen as plain text.
public static text ( mixed $mixed, $addBreaks = true ) : string
$mixed mixed An object, array, or string to be formatted.
return string

textEx() public static method

Process as plain text + our magic formatting.
Since: 2.1
public static textEx ( string $Str ) : string
$Str string
return string

timespan() public static method

public static timespan ( integer $timespan ) : string
$timespan integer
return string

to() public static method

Takes a mixed variable, formats it in the specified format type, and returns it.
public static to ( mixed $Mixed, string $FormatMethod ) : mixed
$Mixed mixed An object, array, or string to be formatted.
$FormatMethod string The method with which the variable should be formatted.
return mixed

toDate() public static method

Format a timestamp or the current time to go into the database.
public static toDate ( integer $Timestamp = '' ) : string
$Timestamp integer
return string The formatted date.

toDateTime() public static method

Format a timestamp or the current time to go into the database.
public static toDateTime ( integer $Timestamp = '' ) : string
$Timestamp integer
return string The formatted date and time.

toTimestamp() public static method

Convert a datetime to a timestamp.
public static toTimestamp ( string $DateTime = '' ) : string | boolean
$DateTime string The Mysql-formatted datetime to convert to a timestamp. Should be in one of the following formats: YYYY-MM-DD or YYYY-MM-DD HH:MM:SS.
return string | boolean Returns FALSE upon failure.

toTimezone() public static method

Formats a timestamp to the current user's timezone.
public static toTimezone ( integer $Timestamp ) : integer
$Timestamp integer The timestamp in gmt.
return integer The timestamp according to the user's timezone.

unembedContent() public static method

This allows later parsing to insert a sanitized video video embed normally. Necessary for backwards compatibility from when we allowed embed & object tags. This is not an HTML filter; it enables old YouTube videos to theoretically work, it doesn't effectively block YouTube iframes or objects.
public static unembedContent ( mixed $Mixed ) : HTML
$Mixed mixed
return HTML string

unserialize() public static method

Takes a serialized variable and unserializes it back into its original state.
public static unserialize ( string $SerializedString ) : mixed
$SerializedString string A json or php serialized string to be unserialized.
return mixed

url() public static method

Creates URL codes containing only lowercase Roman letters, digits, and hyphens.
public static url ( mixed $Mixed ) : string
$Mixed mixed An object, array, or string to be formatted.
return string

vanillaSprintf() public static method

public static vanillaSprintf ( $PlaceholderString, $ReplaceWith ) : mixed
$PlaceholderString
$ReplaceWith
return mixed

vanillaSprintfCallback() protected static method

protected static vanillaSprintfCallback ( $Match, boolean $InternalReplacementList = false ) : mixed
$Match
$InternalReplacementList boolean
return mixed

wysiwyg() public static method

Format text from WYSIWYG editor input.
public static wysiwyg ( $Mixed ) : mixed | string
$Mixed
return mixed | string

Property Details

$DisplayNoFollow public_oe static_oe property

Flag which allows plugins to decide if the output should include rel="nofollow" on any links.
public static $DisplayNoFollow

$MentionsUrlFormat public_oe static_oe property

public static string $MentionsUrlFormat
return string

$SanitizedFormats protected_oe static_oe property

protected static array $SanitizedFormats
return array

$_CleanChars protected_oe static_oe property

Unicode to ascii conversion table.
protected static $_CleanChars

$_UrlTranslations protected_oe static_oe property

protected static array $_UrlTranslations
return array