Reputation: 150
I have used Laravel 5.1 for my current project. In this project, I have a text area input field where user can insert HTML with predefined placeholder. Please take a look:
<div id="recommend">
<div class="title"><p>Title</p></div>
#{item}
<div class="layout">
<div class="item">
<a href="#{url}" class="rcm11"><img border="0" alt="#{name}" src="#{image}"></a>
</div>
</div>
#{/item}
</div>
I saved it to my database. when a user request a HTML, I have given this html by replacing placeholder with proper value. On the above HTML #{item}, #{url} etc. are place holders. I need to parse this place holders and replace with proper value. In Laravel or PHP, how can I do it? If anyone have a answer please let me know.
Upvotes: 2
Views: 54
Reputation: 7785
You can use str_replace() by giving 2 arrays
$placeHolder = array('#{item}' , '#{url}' , ...);
$values = array('itemValue', 'urlValue', ...);
print_r (str_replace($placeHolder, $values, $template));
Or strtr()
$trans = array('#{item}' => 'itemValue',
'#{url}' => 'urlValue' ,
...);
print_r (strtr($template, $trans));
Upvotes: 5