Parsoid
A bidirectional parser between wikitext and HTML5
Loading...
Searching...
No Matches
Wikimedia\Parsoid\Ext\PHPUtils Class Reference

This class contains sundry helpers unrelated to core Parsoid. More...

Static Public Member Functions

static iterable_to_array (iterable $iterable)
 Convert an iterable to an array.
 
static jsonEncode ( $o)
 json_encode wrapper function
 
static stripPrefix ( $subject, $prefix)
 If a string starts with a given prefix, remove the prefix.
 
static stripSuffix ( $subject, $suffix)
 If a string ends with a given suffix, remove the suffix.
 

Detailed Description

This class contains sundry helpers unrelated to core Parsoid.

Member Function Documentation

◆ iterable_to_array()

static Wikimedia\Parsoid\Ext\PHPUtils::iterable_to_array ( iterable $iterable)
static

Convert an iterable to an array.

This function is similar to but not the same as the built-in iterator_to_array, because arrays are iterable but not Traversable!

This function is also present in the wmde/iterable-functions library, but it's short enough that we don't need to pull in an entire new dependency here.

See also
https://stackoverflow.com/questions/44587973/php-iterable-to-array-or-traversable
https://github.com/wmde/iterable-functions/blob/master/src/functions.php

@phan-template T

Parameters
iterable<T>$iterable
Returns
array<T>

◆ jsonEncode()

static Wikimedia\Parsoid\Ext\PHPUtils::jsonEncode ( $o)
static

json_encode wrapper function

  • unscapes slashes and unicode
Parameters
mixed$o
Returns
string

◆ stripPrefix()

static Wikimedia\Parsoid\Ext\PHPUtils::stripPrefix ( $subject,
$prefix )
static

If a string starts with a given prefix, remove the prefix.

Otherwise, return the original string. Like preg_replace( "/^$prefix/", '', $subject ) except about 1.14x faster in the replacement case and 2x faster in the no-op case.

Note: adding type declarations to the parameters adds an overhead of 3%. The benchmark above was without type declarations.

Parameters
string$subject
string$prefix
Returns
string

◆ stripSuffix()

static Wikimedia\Parsoid\Ext\PHPUtils::stripSuffix ( $subject,
$suffix )
static

If a string ends with a given suffix, remove the suffix.

Otherwise, return the original string. Like preg_replace( "/$suffix$/", '', $subject ) except faster.

Parameters
string$subject
string$suffix
Returns
string

The documentation for this class was generated from the following file: