使用PHP和HTML转义字符串 [英] Escape string with PHP and HTML

查看:86
本文介绍了使用PHP和HTML转义字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这两行我想对$ type变量进行转义:

I have these two lines that I would like to escape the $type variable:

$functionName = str_replace('-', '_', $type);
$output .= '<div class="tab-pane" id="'. $type .'">';

我尝试像下面那样进行转义,但它使我感到困惑,不确定那是否正确:

I tried escaping like below but its confusing me and not sure whether thats right:

$output .= '<div class="tab-pane" id="\'. $type .'\">';

推荐答案

示例1:单引号之间的变量

如果使用单引号,则引号之间的所有内容将始终被视为字符串的一部分.

If you use single quotes everything between them will always be treated as part of the string.

$ output.='< div class ="tab-pane" id ='.$ type.'">;

示例2:双引号之间的变量(选项1)

如果您有一个要传递给字符串的变量,则可以使用双引号将其放入其中,而de variable是接触"其他单词.它应该始终有空格.

If you have a variable that you want to pass in a string you can just put it in there if you use double quotes and de variable is nog 'touching' the other words. It should always have spaces.

$output .= "<p>i would like to $your_text_here with you.</p>";

示例3:将字符串中的引号转义

可以通过在要转义的字符之前使用\(反斜杠)来转义字符串中的字符.

Escaping characters in a string can be done by using a \ (backslash) before the character you want to escape.

$output .= "<div class=\"tab-pane\" id=\"example-id\">";

示例4:双引号之间的变量,且其旁边没有空格

如果使用双引号(选项2),则可以将变量放在{}大括号之间

You can place your variable between {} braces if you use double quotes (option 2)

$output .= "<div class=\"tab-pane\" id=\"{$type}\">";

但是,将PHP变量与字符串文字混合

这篇关于使用PHP和HTML转义字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆