简体   繁体   English

在树枝上的字符串上做一个 preg 替换

[英]Make a preg replace on string in twig

I'm trying to find ou a way to make this in twig but I can't find a solution..我试图找到一种方法来在树枝上制作它,但我找不到解决方案..

preg_replace('/(\w+)([A-Z])/U', '\\1 \\2', ucfirst("thisIsAnExample")) ; 

The output in php is "This Is An Example" php 中的输出是“这是一个例子”

Is it possible to do the same thing in twig ?是否可以在 twig 中做同样的事情?

You will need to use a twig extension which will create a custom function for you to use in twig.您将需要使用twig 扩展程序,它将创建一个自定义函数供您在 twig 中使用。

For example:例如:

/**
 * Convert camel case to a capitalised human readable string.
 *
 * @param $camelCase string
 * @return string
 */
public function camelCaseToString($camelCase)
{
    return preg_replace("/([A-Z])/", " $1", ucfirst($camelCase));
}

This should convert thisIsAnExample to This Is An Example .这应该将thisIsAnExample转换为This Is An Example

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

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