php - 用PHP在变量中加粗特定文本?
问题描述
我正在尝试修改项目中的字符串。我正在修改的字符串包含用户输入的段落,并且我试图用用户输入的名词和动词替换特定出现的单词(如名词/动词)。一切都运行良好,除了我试图让用户输入的名词和动词的字体以粗体显示,但是我不知道在哪里加粗文本。任何建议或指示将不胜感激。
<?php
//Variables
$stringFirstVerb = $_POST["stringFirstVerb"];
$stringSecondVerb = $_POST["stringSecondVerb"];
$stringThirdVerb = $_POST["stringThirdVerb"];
$stringFirstNoun = $_POST["stringFirstNoun"];
$stringSecondNoun = $_POST["stringSecondNoun"];
$stringThirdNoun = $_POST["stringThirdNoun"];
$stringFirstAdj = $_POST["stringFirstAdj"];
$stringSecondAdj = $_POST["stringSecondAdj"];
$stringThirdAdj = $_POST["stringThirdAdj"];
$stringParagraph = $_POST["stringParagraph"];
$intNounCount = 0;
$intVerbCount= 0;
$intAdjCount = 0;
//check for user input
$stringNewStr = implode("",array($stringFirstVerb,$stringSecondVerb,$stringThirdVerb,$stringFirstNoun,$stringSecondNoun,$stringThirdNoun,$stringFirstAdj,$stringSecondAdj,$stringThirdAdj)); //consolidates every replacable word into a single string
if( strpos($stringNewStr, ' ' ) !== false ){
echo "No spaces allowed in any Verbs, Nouns, or Adj's";
echo "<br>";
echo "<a href='project1.html'>Link to previous Page</a>";
}
else if( strpos($stringNewStr, '!' ) !== false || strpos($stringNewStr, '.' ) !== false ||
strpos($stringNewStr, ';' ) !== false ||
strpos($stringNewStr, ':' ) !== false ||
strpos($stringNewStr, ',' ) !== false){
echo "No punctuation allowed in any Verbs, Nouns, or Adj's";
echo "<br>";
echo "<a href='project1.html'>Link to previous Page</a>";
}
else //if input is valid, moves onto next step
//checkif paragraph contains 3 of each word types
{
$stringNoun = "NOUN";
$intNounCount = (substr_count($stringParagraph, $stringNoun));
$stringVerb = "VERB";
$intVerbCount = (substr_count($stringParagraph, $stringVerb));
$stringAdj = "ADJ";
$intAdjCount = (substr_count($stringParagraph, $stringAdj));
echo $intNounCount;
echo $intAdjCount;
echo $intVerbCount;
if ($intNounCount !== 3 || $intVerbCount !== 3 || $intAdjCount !== 3){
echo "the paragraph must contain the words VERB, NOUN, and ADJ three times each!";
echo "<br>";
echo "<a href='project1.html'>Link to
previous Page</a>";
}
else //if conditions are met
{
//replace user entered words
$stringParagraph = str_replace('VERB1',
$stringFirstVerb, $stringParagraph);
//replaces the first verb in the paragraph with user entered verb in bold text
$stringParagraph = str_replace('VERB2',
$stringSecondVerb, $stringParagraph);
//replaces the second verb in the paragraph with user entered verb in bold text
$stringParagraph = str_replace('VERB3',
$stringThirdVerb, $stringParagraph);
//replaces the third verb in the paragraph with user entered verb in bold text
$stringParagraph = str_replace('NOUN1',
$stringFirstNoun, $stringParagraph);
//replaces the first verb in the paragraph with user entered verb in bold text
$stringParagraph = str_replace('NOUN2',
$stringSecondNoun, $stringParagraph);
//replaces the second verb in the paragraph with user entered verb in bold text
$stringParagraph = str_replace('NOUN3',
$stringThirdNoun, $stringParagraph);
//replaces the third verb in the paragraph with user entered verb in bold text
$stringParagraph = str_replace('ADJ1',
$stringFirstAdj, $stringParagraph);
//replaces the first verb in the paragraph with user entered verb in bold text
$stringParagraph = str_replace('ADJ2',
$stringSecondAdj, $stringParagraph);
//replaces the second verb in the paragraph with user entered verb in bold text
$stringParagraph = str_replace('ADJ3',
$stringThirdAdj, $stringParagraph);
//replaces the third verb in the paragraph with user entered verb in bold text
echo $stringParagraph; //outputs modified paragraph
echo "<br><br><br>";
echo "<a href='project1.html'>Enter another Madlibs!</a>"; //button to html page
//write paragraph to a txt file
$results = "results.txt";
//$resultsLink = fopen($results, 'a');
$stringFileParagraph = $stringParagraph;
//fwrite($resultsLink,
$stringFileParagraph);
//fclose($resultsLink);
file_put_contents($results,
$stringFileParagraph, FILE_APPEND);
//saves the contents to a textfile, if textfile exists new entry will be added
}//end second else
}//end first else
?>
我一直在尝试使用 str_replace 命令修改 $stringParagraph 中的字符串,但没有成功。简而言之,我要做的就是向用户显示 $stringParagraph 的内容,其中 $stringFirstNoun/SecondNoun 的内容是粗体的。
解决方案
$stringParagraph = str_replace('VERB2',
'<span class="highlight">' . $stringSecondVerb . '</span>', $stringParagraph);
然后高亮类可以用你想要的任何 CSS 来设置替换样式,以便将来轻松更改
另外,您可能值得对变量进行一些清理,否则您也可以直接使用 post 数组。由于您没有使用此代码进行任何数据库工作,因此您的主要风险是有人试图嵌入隐藏脚本,您应该查看 strip_tags 或 preg_replace 以清除不需要的/潜在危险的标签。
就像一个想法一样,您是否可以代替一长串变量构建一个 key => value 数组,然后您可以遍历该数组(甚至更好地编写一个函数)来替换您的字符串,这将使其更容易扩展您是否应该添加额外的输入(例如verb4)。除非您在此处显示的代码之外的某处使用这些变量?