2017-10-13 75 views
0

私は小文字で特殊文字と空白を_(アンダースコア)文字に置き換えたいのですが、それをどのように置き換えるのかは分かりません。外部の図書館を一切使わなくてもいいですか? ここに私のコード - がSluibarenの右@twigで正規表現を検索して文字列を置き換えるには?

{% set special_characters='[email protected]#$%^*()' %} 
{{ special_characters|replace('/[^a-zA-Z0-9\s]/','_') }} 
+2

あなたが書く必要がありますカスタムフィルタ。ここに例があります: https://stackoverflow.com/questions/35602077/how-to-perform-regex-using-twig – Sluibaren

+0

これはかなりのURLを作るためですか?もしそうなら、あなた自身で行うのではなく、いくつかのライブラリを使ってスラグを生成することを検討してください。 – chocochaos

答えて

1

だ、カスタムフィルタを作成する必要がありますが、あなたは小枝でそれをしたい場合は、あなたがそのような何かを行うことができます。

{% set special_characters='[email protected]#$%^*() '|split('') %} 

{% set your_string = '[email protected]#has $many%specials^characters*in(this)exemple'|split('') %} 

{% set your_new_string = "" %} 

{% for char in your_string %} 
    {% if (char in special_characters) %} 
    {% set your_new_string = your_new_string ~ '_' %} 
    {% else %} 
    {% set your_new_string = your_new_string ~ char %} 
    {% endif %} 
{% endfor %} 

{{ your_new_string }} 
関連する問題