Created
October 17, 2016 09:17
-
-
Save imsamthomas/72992155c5b081ca80729ba746e52b78 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$pattern = '/{{([a-zA-Z]{0,10})(.*?)}}/si'; | |
$string = '{{product.name}} aaa {{Product.sku}} aa444a {{Product.price}}'; | |
preg_match_all($pattern, $string, $matches); | |
$helper = new helper(); | |
if(isset($matches[0]) && is_array($matches[0])){ | |
foreach ($matches[0] as $var){ | |
$name = $helper->getAttributeName($var); | |
$object->getData($name); | |
} | |
} | |
class helper{ | |
const MAPPING_DELIMITER = '.'; | |
function getAttributeName($string = null){ | |
if(!$string) return null; | |
$string = str_replace( | |
[ | |
'{{', | |
'}}' | |
], '', $string | |
); | |
$pieces = explode(self::MAPPING_DELIMITER, $string); | |
if(isset($pieces[1])){ | |
return $pieces[1]; | |
} else{ | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment