TOP ▲ itcore TOP プログラムパーツ

uHtmlspecialchars2 htmlspecialchars 空白を に変換する。 | itcore 2018年

PHP

PHP TOP

関数

<?php
// htmlspecialchars 空白を&nbsp;に変換する。
function uHtmlspecialchars2($text) {
  $text = htmlspecialchars($text);
  $text = str_replace("\t", "    ", $text);
  $text = str_replace(" ", "&nbsp", $text);
  return $text;
}
?>

テスト

<?php include_once "uHtmlspecialchars2.func"; ?>
<?php
  echo uHtmlspecialchars2("1 2  3   4\t5");
?>

実行結果

1 2  3   4    5