Skip to content

Instantly share code, notes, and snippets.

@imsamthomas
Created October 17, 2016 09:17
Show Gist options
  • Save imsamthomas/72992155c5b081ca80729ba746e52b78 to your computer and use it in GitHub Desktop.
Save imsamthomas/72992155c5b081ca80729ba746e52b78 to your computer and use it in GitHub Desktop.
<?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