简体   繁体   English

Symfony Twig:截断字符串中未定义的字符

[英]Symfony Twig: Truncate undefined characters from string

I got a string from DB that return the address of an user, I want truncate the first part of this leaving only the City and Country. 我从数据库得到一个返回用户地址的字符串,我想截断其中的第一部分,只留下城市和国家。

Stored strig is like: 存储的strig像:

514 S. Magnolia St. - 32806 - Orlando - FL 

I want it to be: 我希望它是:

Orlando - FL

How I can do using Twig? 如何使用Twig? I got the datas fetching an object using Doctrine 我得到了使用Doctrine获取数据的数据

public function profileAction($query)
{
    $user = $this->getDoctrine()
    ->getRepository('AppBundle:User')
    ->find($query);

    if (!$user) {
        throw $this->createNotFoundException(
            'User '. $user . ' not found.'
        );
    }

    return $this->render('profile/profile.html.twig', [
        'user_info'=>$user,
        ]);
}

I'm getting the values using regular Twig: {{ user_info.address }} 我使用常规的Twig获取值: {{ user_info.address }}

如果地址格式始终相同,则可以依靠Twigsplit过滤器,该过滤器的工作方式与PHP中的explode相同

{{ ('514 S. Magnolia St. - 32806 - Orlando - FL' | split('-', 3))[2] }}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM