目前我有一个文本区域,人们可以插入自己的sql脚本供人们查看,但它目前显示相当平淡。
我想知道是否有一种使用Jquery / Javascript / PHP的方法,当人们从数据库加载一个注释时,它会检查一个单词列表。例如“SELECT”,“FROM”,“WHERE”,“INNER”,“JOIN”,如果它们匹配,则将它们的颜色设置为定义的颜色?
当文本来自数据库时,当笔记显示在屏幕上时,需要进行此操作。所以也许有一些方法可以检查从数据库中提取的单词。
这些说明如下:
if (isset($_POST['noteid']))
{
$showNoteInfo = "SELECT Note, NoteName FROM Notes WHERE NoteID = " . $_POST['noteid'];
$stmt = sqlsrv_query($conn, $showNoteInfo);
}
if (isset($_POST['noteid']))
{
if (empty($_POST['noteid']))
{
$notes = 'No Data';
}
if (sqlsrv_has_rows($stmt))
{
$data = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC);
echo "<div class='custom-font title-container'>
<div class='expand-button-container fa fa-expand' onclick='expandWindow()'></div>
<div id='title-container1'><div class='edit-note fa fa-pencil' onclick='editGeneralNote()'> </div>" . "<div data-toggle='modal' data-target='#editNoteNameModal' class='display-inline'>" . $data['NoteName'] . "</div>" . " <div class='save-note fa fa-thumbs-up' onclick='saveGeneralNote(); submitNoteText();'></div></div>
</div>";
echo "<textarea spellcheck='false' readonly id='ta1'>" . $data['Note'] . "</textarea>";
}
else
{
echo "No data found";
}
}
那么,如何在屏幕上显示数据库中的某些单词的颜色?
如果有人可以提供帮助,我会很感激。
答案 0 :(得分:2)
好老路:
//Your keywords to be highlighted
$keyWord = array("SELECT", "FROM", "WHERE");
//The string to stlylish
$str = "Select * From db";
foreach(explode(" ", $str) as $word)
{
if (in_array(strtoupper($word), $keyWord))
{
echo '<span class="color">' . $word . '</span>';
}
}
如果您希望将时尚流程的结果作为字符串而不是简单的echo
。您可以使用implode
。它是explode
的对立面。你只需要将echo线存储在一个数组中,然后在循环之后内爆数组。在某些方面会产生这样的结果:
//Your keywords to be highlighted
$keyWord = array("SELECT", "FROM", "WHERE");
//The string to stlylish
$str = "Select * From db";
$result = array();
foreach(explode(" ", $str) as $word)
{
if (in_array(strtoupper($word), $keyWord))
{
array_push($result, '<span class="color">' . $word . '</span>');
}
else {
array_push($result, $word);
}
}
$str = implode($result, " ");
echo $str;
答案 1 :(得分:1)
我会用preg_replace()
:
$note = preg_replace('%(SELECT|FROM|WHERE)%m', '<span style="color: green;">$1</span>', $data['Note']);
echo $note;
$1
引用第一个捕获组。有关PHP和regexp引用中正则表达式的更多信息,请参见here。
如何在您的场景中使用它:
if (isset($_POST['noteid']))
{
$showNoteInfo = "SELECT Note, NoteName FROM Notes WHERE NoteID = " . $_POST['noteid'];
$stmt = sqlsrv_query($conn, $showNoteInfo);
}
if (isset($_POST['noteid']))
{
if (empty($_POST['noteid']))
{
$notes = 'No Data';
}
if (sqlsrv_has_rows($stmt))
{
$data = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC);
echo "<div class='custom-font title-container'>
<div class='expand-button-container fa fa-expand' onclick='expandWindow()'></div>
<div id='title-container1'><div class='edit-note fa fa-pencil' onclick='editGeneralNote()'> </div>" . "<div data-toggle='modal' data-target='#editNoteNameModal' class='display-inline'>" . $data['NoteName'] . "</div>" . " <div class='save-note fa fa-thumbs-up' onclick='saveGeneralNote(); submitNoteText();'></div></div>
</div>";
$note = preg_replace('%(SELECT|FROM|WHERE)%m', '<span style="color: green;">$1</span>', $data['Note']);
echo "<textarea spellcheck='false' readonly id='ta1'>$note</textarea>";
}
else
{
echo "No data found";
}
}
&#13;