du8794 2017-08-02 10:02
浏览 66
已采纳

Smarty substr&strpos从最后一次出现

{assign var="bar_at" value=$product.supplier_reference|strpos:"="}

The $product.supplier_reference looks like https://www.example.com/search?w=1366&h=610&q=the_actual_reference

I need to get what comes after the last '='(the actual reference)

How to get strpos after the last occurrence?

  • 写回答

1条回答 默认 最新

  • dpkt31779 2017-08-02 11:39
    关注

    First of all the right function to go with is : strrpos

    you have various ways to accomplish this:

    1- the pure smarty way:

    {assign var="str" value="https://www.example.com/search?w=1366&h=610&q=the_actual_reference"}
    {assign var="offset" value=$str|strrpos:"="}
    {assign var="reference" value=$str|substr:($offset+1)}
    {$reference}
    

    2- create new plugin in the plugins directory , which lays under the following path : vendor/smarty/smarty/libs/plugins/ , add new file with your new plugin name, lets say function.getReference.php

    create a new function smarty_function_getReference

    and write your pure PHP function, then use this from your smarty template directly like following :

    function  smarty_function_money ($paramters) {
        $url = $paramters['url'];
        // here is our function body
    }
    

    within your smarty template:

    {getReference url="https://www.example.com/search?w=1366&h=610&q=the_actual_reference"}
    

    3- add new modifier:

    where-ever you are defining your smarty view logic register the new modifier :

    $callback = function ($string) {
        // perform your logic within this callback
    };
    
    $this->registerPlugin('modifier', 'getRefernce', $callback);
    

    then call this modifier directly from your smarty template like following :

    {"https://www.example.com/search?w=1366&h=610&q=the_actual_reference"|getRefernce}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据