How to add span tags to a set of specified keywords by php
This article will explain in detail how php adds span tags to a set of specified keywords. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
The details are as follows:
Here, php adds span tags to a specified set of keywords to highlight the keywords.
/ / Example use: $spanned = codeWords ($string_containing_keywords); / / My site: andrew.dx.am// Using colour==blue, but different arrays of words and different / / colours can be added.function onlyWholeWords (& $value, $key) {/ / Ignores words after / / comment delimiters.//$value = "/\ b (". $value. ")\ b /"; / / doesn't handle comments//$value = "/ ^ (?: (?!\ /).) *\ K\ b (". $value. ")\ b /"; / /\ K lookbehind alternative is not supported in PHP < 5.2.4, so use: $value = "/ ^ ((?: (?!\ /\ /).) *)\ b". $value. "\ b /";} function addSpan (& $value, $key, $color='blue') {$value = "$1". $value. " } function codeWords ($code) {$keywords = array ('as',' break', 'case',' class', 'continue',' default', 'do',' elif', 'else',' elseif', 'for',' foreach', 'function',' if', 'new',' null', 'return',' self', 'switch',' this', 'to',' typeof', 'until' 'var',' void', 'while',' with') $keywords2 = $keywords; array_walk ($keywords, 'onlyWholeWords'); array_walk ($keywords2,' addSpan', 'blue'); $code = preg_replace ($keywords, $keywords2, $code); return $code } this is the end of the article on "how php adds span tags to a set of specified keywords". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.