Mecha CMS

Mecha CMS blog and documentation.

Class Text

Updated: Sunday, 07 August 2016

Text parsers.

Text Parser

Parse Text into ASCII Character

echo Text::parse('email@domain.com', '->ascii');

Result:

email@domain.com

Parse Text into Encoded URL

echo Text::parse('some text (test)', '->encoded_url');

Result:

some+text+%28test%29

Parse Encoded URL into Decoded URL

echo Text::parse('some+text+%28test%29', '->decoded_url');

Result:

some text (test)

Parse Text into Encoded HTML

echo Text::parse('<span>some text</span>', '->encoded_html');

Result:

&lt;span&gt;some text&lt;/span&gt;

Parse Encoded HTML into Decoded HTML

echo Text::parse('&lt;span&gt;some text&lt;/span&gt;', '->decoded_html')';

Result:

<span>some text</span>

Parse Markdown Pattern into HTML

echo Text::parse('some **text**', '->html');

Result:

<p>some <strong>text</strong></p>

Parse Text into Encoded JSON

echo Text::parse('<span>some "text"</span>', '->encoded_json');

Result:

"<span>some \"text\"<\/span>"

Parse Text into Decoded JSON

echo Text::parse('"<span>some \"text\"<\/span>"', '->decoded_json');

Result:

<span>some "text"</span>

Parse Text into URL Slug

Note: Since version 1.1.0, if the resulted data is empty, then this method will automatically returned it as -- for safety reason. Example case is Text::parse('!@#$%^&*()', '->slug').

echo Text::parse('Some text.', '->slug');

Result:

some-text

Parse HTML or URL Slug into Text

echo Text::parse('<strong>yo!</strong> some-text---text+wow', '->text');

Result:

yo! some text-text wow

Parse Text into Array Key

Note: Since version 1.1.0, if the resulted data is empty, then this method will automatically returned it as __ for safety reason. Example case is Text::parse('!@#$%^&*()', '->array_key').

echo Text::parse('some text.', '->array_key');
echo Text::parse('Some Text.', '->array_key', true); // force lower-case

Result:

some_text
some_text

Parse Text into Camel Case

Added since version 1.1.3.

echo Text::parse('Lorem ipsum dolor sit amet', '->camel_case');

Result:

loremIpsumDolorSitAmet

Parse Text into Pascal Case

Added since version 1.1.3.

echo Text::parse('Lorem ipsum dolor sit amet', '->pascal_case');

Result:

LoremIpsumDolorSitAmet

Parse Text into Snake Case

Added since version 1.1.3.

echo Text::parse('LoremIpsumDolorSitAmet', '->snake_case');

Result:

lorem_ipsum_dolor_sit_amet

Parse Text into File Name

Added since version 1.1.3.

echo Text::parse('This is a File Name.JPG', '->safe_file_name');

Result:

this-is-a-file-name.jpg

Safe Folder Name

Added since version 1.2.0.

echo Text::parse($input, '->safe_folder_name');

Safe Path Name

Added since version 1.2.0.

echo Text::parse($input, '->safe_path_name');

Obfuscate Text

Added since version 1.1.4.

echo Text::parse('I Love You', '->broken_entity');

Result:

&#x49;&#x20;&#76;&#111;&#118;&#101;&#x20;&#x59;&#x6f;&#117;

Fake Title

Added since version 1.2.0.

echo Text::parse('lorem ipsum dolor sit amet', '->title');

Result:

Lorem Ipsum Dolor Sit Amet

Creating Your Own Parser

Added since version 1.1.2.

Text::parser('to_lower_case', function($input) {
    return strtolower($input);
});

Usage:

echo Text::parse('Mecha CMS', '->lower_case');

Check for the Available Parser

Added since version 1.1.2.

if(Text::parserExist('to_html')) {
    …
}

Text Checker

Check if $text Contains A

if(Text::check($text)->has('A')) { … }

Check if $text Contains A and B

if(Text::check($text)->has('A', 'B')) { … }

Check if A is in $text

if(Text::check('A')->in($text)) { … }

Check if A or B is in $text

if(Text::check('A', 'B')->in($text)) { … }

Check if $bucket Contains A

This should be faster than in_array('A', array('A', 'B', 'C'))

$bucket = array('A', 'B', 'C');
if(Text::check('A')->in($bucket)) { … }

Check Offset of a String Inside a Text

$offset = Text::check($text)->offset('A');
var_dump($offset->start, $offset->end);
Donation and Email Subscription