JavaScript/htmlspecialchars_decode

更新日 5日前(Asia/Tokyo) 更新者: y963@rogarithm

JavaScript/htmlspecialchars_decode

php側でhtmlspecialcharsをかけた結果を、JSで元に戻す

例えばhtmlspecialchars_decode(“”)

function htmlspecialchars_decode(str) {
	if (typeof str !== 'string') return str;
	return str
		.replace(/</g, '<')    // < → <
		.replace(/>/g, '>')    // > → >
		.replace(/"/g, '"')  // " → "
		.replace(/'/g, "'")  // ' → '
		.replace(/&/g, '&');  // & → &
}

// あるいは(こっちのほうがいい)

function htmlspecialchars_decode(str) {
  const parser = new DOMParser();
  const decodedString = parser.parseFromString(str, 'text/html').body.textContent;
  return decodedString;
}
パス: JavaScript/htmlspecialchars_decode.md
最終更新: 2025-11-30 21:54:30(Asia/Tokyo)