Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

substr - string starts with letters or not

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
haldonclark
Smarty n00b


Joined: 04 Oct 2020
Posts: 2

PostPosted: Sun Oct 04, 2020 6:17 am    Post subject: substr - string starts with letters or not Reply with quote

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
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Mon Oct 05, 2020 3:41 am    Post subject: Reply with quote

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
View user's profile Send private message
bsmither
Smarty Elite


Joined: 20 Dec 2011
Posts: 322
Location: West Coast

PostPosted: Tue Oct 06, 2020 11:29 pm    Post subject: Reply with quote

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
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Wed Oct 07, 2020 3:33 am    Post subject: Reply with quote

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
View user's profile Send private message
haldonclark
Smarty n00b


Joined: 04 Oct 2020
Posts: 2

PostPosted: Mon Oct 26, 2020 1:31 pm    Post subject: Many Thanks Reply with quote

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
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
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
Protected by Anti-Spam ACP