|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
haldonclark Smarty n00b
Joined: 04 Oct 2020 Posts: 2
|
Posted: Sun Oct 04, 2020 6:17 am Post subject: substr - string starts with letters or not |
|
|
Hi there,
am I able to find out if a string starts with 2 letters?
For example, I would like to find out if the string CH123.234.212 starts with 2 letters of the alphabet instead of digits.
Thanks for your input! |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Mon Oct 05, 2020 3:41 am Post subject: |
|
|
Use regular expressions. I.e. https://www.php.net/preg_match
Not a Smarty question.
Last edited by AnrDaemon on Wed Oct 07, 2020 3:30 am; edited 1 time in total |
|
Back to top |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Tue Oct 06, 2020 11:29 pm Post subject: |
|
|
To have Smarty test your specific example:
Code: | {if $test|regex_replace:"/^[A-Za-z]{ldelim}2{rdelim}/":"" !== $test}Modified{else}Not Changed{/if} |
Using Smarty's regex_replace, we examine the first two characters and if they both are letters, then they are removed. Otherwise, the value of the variable is unchanged.
The regex pattern includes the syntax {2} to designate that exactly two characters must be found in the preceding token (which is the bracketed collection).
However, because Smarty uses braces for itself as delimiters, we need to use Smarty's solution to use an actual brace:
{ldelim}2{rdelim}
The typical "not-a-delimiter" solution does not work here. That "not-a-delimiter" solution is to have white-space on the insides of the braces:
{ 2 }
Unfortunately, this breaks REGEX parsing. |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Wed Oct 07, 2020 3:33 am Post subject: |
|
|
Since you are using if, you could just write preg_match directly.
Code: | {if preg_match("/^[A-Za-z]{ldelim}2{rdelim}/", $test)} … {/if} |
|
|
Back to top |
|
haldonclark Smarty n00b
Joined: 04 Oct 2020 Posts: 2
|
Posted: Mon Oct 26, 2020 1:31 pm Post subject: Many Thanks |
|
|
I just now discovered that you guys responded, despite the fact that I enabled notifications. Great thanks! This logic works.
bsmither wrote: | To have Smarty test your specific example:
Code: | {if $test|regex_replace:"/^[A-Za-z]{ldelim}2{rdelim}/":"" !== $test}Modified{else}Not Changed{/if} |
Using Smarty's regex_replace, we examine the first two characters and if they both are letters, then they are removed. Otherwise, the value of the variable is unchanged.
The regex pattern includes the syntax {2} to designate that exactly two characters must be found in the preceding token (which is the bracketed collection).
However, because Smarty uses braces for itself as delimiters, we need to use Smarty's solution to use an actual brace:
{ldelim}2{rdelim}
The typical "not-a-delimiter" solution does not work here. That "not-a-delimiter" solution is to have white-space on the insides of the braces:
{ 2 }
Unfortunately, this breaks REGEX parsing. |
|
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|