从字符串的开头和结尾删除{{和}}

时间:2018-02-15 17:32:58

标签: php

我试图强迫这个......

{{ blogPost::author }}

设置......

$componentAlias = "blogPost";

$property = "author";

我目前有......

list($componentAlias, $property) = explode('::', $this->property('identifierValue'));

但是我想事先从字符串的两边修剪{{和}},包括空格。

我尝试过使用ltrim('{{ ', $componentAlias)等解决方案,但似乎无法正常工作。

3 个答案:

答案 0 :(得分:1)

trim("{{ blogPost::author }}","{{  }}");

这应该删除前导和尾随" {{"和" }}"

您也可以尝试

str_replace("{{ ","","{{ blogPost::author }}");

答案 1 :(得分:0)

这个怎么样

$string = '{{ blogPost::author }}';
$new_string =  rtrim(ltrim($string, '{{'), '}}');

答案 2 :(得分:0)

对于您的示例,您可以使用trim并列出要删除的所有字符作为第二个参数:

var_dump(trim("{{ blogPost::author }}", "{ }"));;

那会给你:

  

string(16)“blogPost :: author”