用PHP在变量中加粗特定文本?
我正在尝试修改项目中的字符串。我正在修改的字符串包含用户输入的段落,我试图用用户输入的名词和动词替换特定出现的单词(如名词/动词)。一切都很好,除了我试图让用户输入的名词和动词的字体变为粗体,但是我不知道在哪里加粗文本。如有任何建议或建议,将不胜感激用PHP在变量中加粗特定文本?,php,fonts,Php,Fonts,我正在尝试修改项目中的字符串。我正在修改的字符串包含用户输入的段落,我试图用用户输入的名词和动词替换特定出现的单词(如名词/动词)。一切都很好,除了我试图让用户输入的名词和动词的字体变为粗体,但是我不知道在哪里加粗文本。如有任何建议或建议,将不胜感激 <?php //Variables $stringFirstVerb = $_POST["stringFirstVerb"]; $stringSecondVerb = $_POST["stringSecondVer
<?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
?>
当您用相应的值替换令牌(如VERB1
)时,您希望将该值用粗体标记包装。因此:
$stringParagraph = str_replace('VERB1',
$stringFirstVerb, $stringParagraph);
应该是:
$stringParagraph = str_replace('VERB1',
"<b>".$stringFirstVerb."</b>", $stringParagraph);
$stringparagration=str_replace('VERB1',
“.$stringFirstVerb.”,$String段落);
$stringparagration=str\u replace('VERB2',
“.$stringSecondVerb.”,$String段落);
然后,highlight类可以使用您想要的任何CSS为替换设置样式,以便将来进行更改
另外,您可能需要对变量进行一些清理,否则您也可以直接使用post数组。由于您没有使用此代码进行任何数据库工作,您的主要风险是有人试图嵌入隐藏的脚本,因此您应该查看strip_标记或preg_替换以清除不需要的/潜在危险的标记
正如一个想法,你可以不用一长串的变量来构建一个key=>value数组,然后你可以在数组中循环(或者更好地编写一个函数)来替换字符串,如果你添加额外的输入(例如verb4),这将使它更容易扩展。除非你在这里展示的代码之外使用这些变量?我真的不太明白你的问题?
如果用户以小写形式输入名词/动词,您可能希望执行操作,但如果用户输入的名词/动词带有用户输入的其中一个字母,则大写操作失败
在组合所有变量之前,可以使用strtolower()函数/
试试这个:
$stringNewStr = implode ("", array ($ stringFirstVerb, $ stringSecondVerb, $stringThirdVerb, $ stringFirstNoun, $ stringSecondNoun, $ stringThirdNoun, $stringFirstAdj, $ stringSecondAdj, $ stringSecondAdj, $ stringThirdAdj));
$stringNewStrLow = strtolower($stringNewStr);
注意:根据HTML5规范,当没有其他更合适的标记时,该标记应作为最后手段使用。HTML5规范规定标题应使用to标记表示,强调文本应使用标记表示,重要文本应使用标记表示,标记/突出显示的文本应使用标记。我将代码应用到我的行中,它们没有受到影响。我将更深入地研究span类,看看是否可以使其正常工作。谢谢你的建议。更新:代码运行得很好,非常感谢您的帮助!需要明确的是,span是一个html标记,类是用于CSS的,我假设您知道如何使用CSS和CSS类等-highlight类是示例的任意名称,不会自动执行任何操作。如果不是这样的话,那么就这么说,我会更新答案您的变量无效,$
和变量名之间不能有空格。另外,请使用较新的缩写[]
代替array()
。
$stringNewStr = implode ("", array ($ stringFirstVerb, $ stringSecondVerb, $stringThirdVerb, $ stringFirstNoun, $ stringSecondNoun, $ stringThirdNoun, $stringFirstAdj, $ stringSecondAdj, $ stringSecondAdj, $ stringThirdAdj));
$stringNewStrLow = strtolower($stringNewStr);