I’m trying to parse some input using regex. The input will be in the format:
{somevalue:3}
The aim is to display ‘som’ (no quotemarks).
At the moment, I have:
'echo' => array(
'search' => '~\{((?:'.$this->preg['var'].')(?:\.)?)\}~sU',
'replace' => "'.\$this->_get('\\1').'"
)
Which works great with my template system, to echo the standard variable (i.e. ‘somevalue’). However, I wish to allow the user to use the : delimiter to limit the number of characters to output (i.e. {somevalue:3} would display ‘som’).
I tried:
'echo' => array(
'search' => '~\{((?:'.$this->preg['var'].')(?:\.)?:(.*)/)\}~sU',
'replace' => "'.substr(\$this->_get('\\1'),0,\\2).'"
)
But this didn’t work. I don’t really understand regex to be honest so any help would be much appreciated.
It looks like you have an extra ‘/’ in the new search expression.
I’m not familiar with the templating system you’re using, but it appears that the replace expression would need to be changed as well.
Put these together and you’d get something like this to try:
It should be noted that if this works, the old way of doing input won’t work anymore. In other words, you’ll always have to use the format
{<string>:<num_of_chars>}and not just{<string>}.