joe codes

x-cart guru & custom programmer

  • About
  • Archives
  • Contact

Powered by Genesis

Smarty preg_match

September 24, 2018 by joecodes Leave a Comment

This was not obvious to me nor apparently anyone else trying to use preg_match in Smarty so I thought I’d share the solution. Given $href=’home.php?cat=1′ and trying to get 1 out of it:

{if "/cat=(\d+)/"|preg_match:$href:$match}
  {$href = "My-Page-`$match.1`.html"}
{/if}

preg_match returns the number of times it matched so we can use it directly as our condition then it dumps all of the matches into the $match array which looks like this:

    [0] => cat=1
    [1] => 1

The key is that you can pass all of the parameters separated by colon (:) as mentioned in the Smarty docs:

parameters follow the modifier name and are separated by a : (colon)

But that’s as far as the docs go and they don’t provide examples of more advanced PHP functions with multiple parameters, especially those who use a parameter as a return variable. Even the admins of the Smarty forums only pointed to creating a plugin, probably because they feel you should be doing this at a higher level instead of in Smarty, which is a fair point.

This was originally done with an old version of Smarty which also works except for the condition shortcut but this may also help spell it out a little:

{assign var='matched' value="/cat=(.+)/"|preg_match:$href:$match}
{if $matched}
  {assign var='href' value="My-Page-`$match.1`.html"}
{/if}

You can also use PHP functions in Smarty like the following, but $match did not have our matching array when working with the old version of Smarty but it does work with Smarty 3:

{if preg_match("/cat=(.+)/", $href, $match)}

If you are dealing with a greedy match, you can also use preg_match_all. If I had been working with a newer version of Smarty my initial try with this last method would have worked and I may have stopped there but hopefully this exercise helps someone.

Share this:

  • Click to share on Twitter (Opens in new window)
  • Click to share on Reddit (Opens in new window)
  • Click to share on Facebook (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to email this to a friend (Opens in new window)

Related

Filed Under: Programming Tagged With: Smarty

Leave a Reply Cancel reply

You must be logged in to post a comment.

Quick Thoughts

  • I was surprised to learn that foreach in JavaScript does not have a traditional break. The loop will run to completion.

  • Who knew that combination sums across all permutation lengths of an array would be so difficult? It was a challenge but the final product looks good and takes a lot of resources. Limiting the max length for basic memory limits. Would only do something like this for occasional reporting.

  • Working on a new project that can have hundreds of forms on a page. The browser was spending way too much time in Parse HTML. Wasted a bunch of time before learning this is a long-standing bug in Chrome when there are many forms or inputs. Other browsers are fine.

Recent Posts

  • MacBook External DVD Player
  • Progressive Enhancement
  • Keychain Password Search
  • Smarty preg_match
  • iPhone Plus Experiment

Tag Cloud

Apple JavaScript Mason Perl PHP security simple Smarty speed stability Tax WWDC X-Cart

Search

Subscribe

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Recent Comments

  • Bad App Alert on Startup Item Help
  • iPhone Pre-Order Needs to Change on iPhone Pre-Order Warning
  • Apple vs Pro on My Personal Three S’s of Development
loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.