Php 从字符串的开头和结尾删除{和}}
我正试图强迫这个Php 从字符串的开头和结尾删除{和}},php,Php,我正试图强迫这个 {{ blogPost::author }} 设置 $componentAlias = "blogPost"; $property = "author"; 我现在有 list($componentAlias, $property) = explode('::', $this->property('identifierValue')); 但是我想先从字符串的两边修剪{{和}},包括空格 我尝试了一个使用ltrim('{{',$componentAlias)等的解决方
{{ blogPost::author }}
设置
$componentAlias = "blogPost";
$property = "author";
我现在有
list($componentAlias, $property) = explode('::', $this->property('identifierValue'));
但是我想先从字符串的两边修剪{{和}},包括空格
我尝试了一个使用ltrim('{{',$componentAlias)
等的解决方案,但似乎不起作用
trim("{{ blogPost::author }}","{{ }}");
这应该删除前导和尾随的“{{”和“}}”
你也可以试试
str_replace("{{ ","","{{ blogPost::author }}");
这个怎么样
$string = '{{ blogPost::author }}';
$new_string = rtrim(ltrim($string, '{{'), '}}');
对于您的示例,您可以使用并列出要剥离的所有字符作为第二个参数:
var_dump(trim("{{ blogPost::author }}", "{ }"));;
这将给你:
字符串(16)“blogPost::author”
字符串应该是任何trim变体中的第一个参数,后跟不需要的字符