使用php自定义文本格式

使用php自定义文本格式,php,html,formatting,Php,Html,Formatting,使用PHP,我如何将HTML字符串(比如,'+text+'转换为'text'),以便用户在输入中键入+text+,它将输出为文本?您可以使用str_replace-along-side-explode或regex $text_string = '+TEST+'; $text = explode("+", $text_string); $text = $text[1]; $replaced_text = str_replace("+$text+", "<em>$text</em&

使用PHP,我如何将HTML字符串(比如,
'+text+'
转换为
'text'
),以便用户在输入中键入+text+,它将输出为文本?

您可以使用str_replace-along-side-explode或regex

$text_string = '+TEST+';
$text = explode("+", $text_string);
$text = $text[1];
$replaced_text = str_replace("+$text+", "<em>$text</em>", $text_string);
echo $replaced_text; // output: <em>TEST</em>
$text_string='+TEST+';
$text=分解(“+”,$text\u字符串);
$text=$text[1];
$replaced_text=str_replace(“+$text+”、“$text”、$text_字符串);
echo$replaced_text;//输出:测试

如果你只想使用你能做的

$input = "+text+";

$input = trim($input, '+');

$input = '<em>' . $input . '</em>';
$input=“+text+”;
$input=trim($input,“+”);
$input=''$输入";;

+text+
插入到输入中,而
text
存储在数据库表中。这可能效率低下,因为只有当+和+是字符串中的第一个和最后一个字母时,这才有效,我很确定,如果用户正在输入,他的字符串不会是这样。很难说问题描述到底发生了什么,但你是对的。但是,如果他们能够确保+始终位于开始和结束位置,则速度会快得多。然而,优雅的解决方案是否适用于多次出现的字符串?例如:
+test+是一件非常棒的事情