Mecha CMS

Mecha CMS blog and documentation.

Class Converter

Updated: Sunday, 07 August 2016

Convert this into that.

Added since version 1.0.3.

Extract RGBA Colors from RGB/RGBA Color String

var_dump(Converter::RGB('rgb(255, 255, 255)'));
var_dump(Converter::RGB('rgba(255, 255, 255, .5)'));
var_dump(Converter::RGB('rgba(255, 255, 255, 0.5)'));

Results:

array(
    'r' => 255,
    'g' => 255,
    'b' => 255,
    'a' => 1
);

array(
    'r' => 255,
    'g' => 255,
    'b' => 255,
    'a' => 0.5
);

array(
    'r' => 255,
    'g' => 255,
    'b' => 255,
    'a' => 0.5
);

Optional to output only single part of the generated color:

echo Converter::RGB('rgb(255, 255, 255, .5)', 'a');

Results:

0.5

Convert Hexadecimal Color String into RGB Data

var_dump(Converter::HEX2RGB('#ffa500'));

Results:

array(
    'r' => 255,
    'g' => 165,
    'b' => 0,
    'a' => 1
);

Optional to output only single part of the generated color:

echo Converter::HEX2RGB('#ffa500', 'r');

Results:

255

Convert RGB Colors into Hexadecimal String

echo Converter::RGB2HEX(255, 255, 255);
echo Converter::RGB2HEX(array(255, 255, 255));
echo Converter::RGB2HEX('rgb(255, 255, 255)');
echo Converter::RGB2HEX('rgba(255, 255, 255, .4)');

Results:

#ffffff
#ffffff
#ffffff
#ffffff

Summarize the Long Text Input

Added since version 1.1.2.

$input = 'Very very very long text...';
$chars = 200;
$tail = '…';

echo Converter::curt($input, $chars, $tail);

Convert String of Value into Their Appropriate Type/Format

$test = array(
    'Test String' => 'Lorem Ipsum',
    'Test Numeric 1' => '12345',
    'Test Numeric 2' => '1234.5',
    'Test Numeric 3' => '.5',
    'Test Numeric 4' => '0.5',
    'Test Numeric Force String' => '"0.5"',
    'Test Boolean 1' => 'true',
    'Test Boolean 2' => 'false',
    'Test Boolean 3' => 'yes',
    'Test Boolean 4' => 'no',
    'Test Boolean 5' => 'on',
    'Test Boolean 6' => 'off',
    'Test Boolean Force String' => '"true"',
    'Test Null' => 'null',
    'Test Null Force String' => '"null"',
    'Test Nested Array' => array(
        '1',
        '2',
        'true',
        'this' => array(
            'is' => 'null'
        )
    ),
    'Test JSON' => '{"str":"foo","bool":"true","int":"123","float":"0.7"}',
);

$results = Converter::strEval($test);

Results for $results:

array(
    'Test String' => 'Lorem Ipsum',
    'Test Numeric 1' => 12345,
    'Test Numeric 2' => 1234.5,
    'Test Numeric 3' => 0.5,
    'Test Numeric 4' => 0.5,
    'Test Numeric Force String' => '0.5',
    'Test Boolean 1' => true,
    'Test Boolean 2' => false,
    'Test Boolean 3' => true,
    'Test Boolean 4' => false,
    'Test Boolean 5' => true,
    'Test Boolean 6' => false,
    'Test Boolean Force String' => 'true',
    'Test Null' => null,
    'Test Null Force String' => 'null',
    'Test Nested Array' => array(
        0 => 1,
        1 => 2,
        2 => true,
        'this' => array(
            'is' => null
        ),
    ),
    'Test JSON' => array(
        'str' => 'foo',
        'bool' => true,
        'int' => 123,
        'float' => 0.7
    )
);

Single string also accepted:

$test = Converter::strEval('true');

Convert PHP Value into Their String Format

Added since version 1.1.3.

$test = array(
    'Test Boolean 1' => true,
    'Test Boolean 2' => false,
    'Test Null' => null,
    'Test Numeric 1' => 12345,
    'Test Numeric 2' => 1234.5
);

$results = Converter::str($test);

Results for $results:

array(
    'Test Boolean 1' => 'true',
    'Test Boolean 2' => 'false',
    'Test Null' => 'null',
    'Test Numeric 1' => '12345',
    'Test Numeric 2' => '1234.5'
);

Single value also accepted:

$test = Converter::str(true);

Convert String of Value into Executable PHP Code

echo Converter::phpEval('echo time() + 15;');

Convert String of Element into Array of Data

Added since version 1.0.4.

$data_1 = Converter::attr('<div class="foo" data-test="true" foo="1000">');
$data_2 = Converter::attr('<div id="foo">test content</div>');

Results:

$data_1 = array(
    'element' => 'div',
    'attributes' => array(
        'class' => 'foo',
        'data-test' => true,
        'foo' => 1000
    ),
    'content' => null
);

$data_2 = array(
    'element' => 'div',
    'attributes' => array(
        'id' => 'foo'
    ),
    'content' => 'test content'
);

Options:

Converter::attr($input, $element, $attr, $str_eval);
ParameterDescription
$inputThe string element to be converted.
$elementDefault value is array('<', '>', ' ') where < represent the start of the element, > represent the end of the element and the last value represent the separator between the element’s name and element’s attributes.
$attrDefault value is array('"', '"', '=') where " represent the start and end of the attribute value and = represent the separator/connector of attribute to their value.
$str_evalChange the value into false to preserve all attribute value type as string.

Example using different settings to convert different element format/pattern:

$data = Converter::attr(
    '[div=class:`foo`=id:`bar`]',
    array('[', ']', '='),
    array('`', '`', ':')
);

Example for extracting shortcode attributes:

$shortcode_data = Converter::attr(
    '{{shortcode test:null id:bar count:3000}}',
    array('{{', '}}', ' '),
    array("", "", ':')
);

Results for $shortcode_data:

array(
    'element' => 'shortcode',
    'attributes' => array(
        'test' => null,
        'id' => 'bar',
        'count' => 3000
    ),
    'content' => null
);

Tests code to show all results from possible patterns:

$test = array(
    '<div class="foo" id="bar" data-test="1000">',
    '<div>',
    '<div class="foo" id="bar" data-test="1000">test content</div>',
    '<div>test content</div>',
    '<div>test content</span>',
    '<div>test content',
    '<div></div>',
    '<div class="foo" id="bar" data-test="1000"/>',
    '<div class="foo" id="bar" data-test="1000" />',
    '< div  class="foo"     id="bar"   data-test="1000"       />',
    '<div class id data-test>',
    '<id="foo" data-test="1000">',
    '<id data-test>',
    '<select name="foo" id="bar" empty-value-test="" selected disabled><option value="1">Option 1</option></select>'
);

foreach($test as $t) {
    var_dump($t, Converter::attr($t));
    echo '<hr>';
}

Minify HTML Output

Added since version 1.1.2.

echo Converter::detractSkeleton($source_code);

Minify CSS Output

Added since version 1.1.2.

echo Converter::detractShell($source_code);

Minify JavaScript Output

Added since version 1.1.2.

echo Converter::detractSword($source_code);

Convert Array into Nested Text Format

Added since version 1.1.3.

$test = array(
    'foo' => 'bar',
    'baz' => 'qux',
    'test' => array(
        'a' => 'b',
        'c' => array(1, 2, 3, 4)
    ),
    'test test' => 'Yo!'
);

$results = Converter::toText($test);

Results for $results:

foo: bar
baz: qux
test:
    a: b
    c:
        0: 1
        1: 2
        2: 3
        3: 4
test test: Yo!

Related: YAML

Convert Nested Text Format into Array

$text = 'Key 1: Value 1
Key 2: Value 2';

var_dump(Converter::toArray($text));

Results:

array(
    'Key 1' => 'Value 1',
    'Key 2' => 'Value 2'
);
Donation and Email Subscription