Khaled_Saiful_Islam
Khaled_Saiful_Islam

Reputation: 150

HTML parsing with relevant value in PHP

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

Answers (1)

Halayem Anis
Halayem Anis

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

Related Questions