fork download
  1. <?php
  2.  
  3. function parseAttributes(array $htmlOptions=array())
  4. {
  5. $res = '';
  6. foreach($htmlOptions as $option => $value)
  7. $res .= $option . '="' . str_replace('"', '\\"', $value) . '" ';
  8. return htmlspecialchars(trim($res), ENT_NOQUOTES);
  9. }
  10. function tag($tag, array $htmlOptions=array(), $content=false, $encodeContent=false, $withCloseTag=true)
  11. {
  12. $tag = htmlspecialchars($tag);
  13. $html='<' . trim($tag . ' ' . parseAttributes($htmlOptions));
  14. if($content===false)
  15. return $withCloseTag ? $html . '></' . $tag . '>' : $html . '>';
  16. if($encodeContent)
  17. $content = htmlspecialchars($content, ENT_NOQUOTES);
  18. return $withCloseTag ? $html . '>' . $content . '</' . $tag . '>' : $html . '/>' . $content;
  19. }
  20.  
  21. tag('a', array('href'=>'http://f...content-available-to-author-only...o.bar', 'class'=>'foo', 'id' => 'bar')),
  22. tag('span', array('style' => 'foo=bar;lorem=dolor;', 'name'=>'"barr"')),
  23. tag('div', array('class' => 'test'), 'работает'),
  24. tag('br', array(), false, false, false)
  25. );
Success #stdin #stdout 0.02s 24400KB
stdin
Standard input is empty
stdout
string(50) "<a href="http://f...content-available-to-author-only...o.bar" class="foo" id="bar"></a>"
string(58) "<span style="foo=bar;lorem=dolor;" name="\"barr\""></span>"
string(40) "<div class="test">работает</div>"
string(4) "<br>"