PHP adds a span label to a set of keywords _php tips

Source: Internet
Author: User
Tags php programming

The example in this article describes how PHP adds a span label to a set of specified keywords. Share to everyone for your reference. Specifically as follows:

Here is PHP to add a span label to a set of keywords, highlighting the keyword

Example use: $spanned = codewords ($string _containing_keywords);
My site:andrew.dx.am//Using colour==blue, but different arrays to 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 isn't supported in PHP < 5.2.4, so use: $value = "/^ (?:(?! \/\/).) *) \b ". $value.
"\b/"; Function Addspan (& $value, $key, $color = ' blue ') {$value = ' $1<span style= ' color: $color ' > '. $value.
"</span>"; function codewords ($code) {$keywords = array (' As ', ' break ', ' case ', ' class ', ' Continue ', ' default ', ' Do ', ' elif ', ' Else ', ' ElseIf ', ' for ', ' foreach ', ' function ', ' if ', ' new ', ' null ', ' return ', ' self ', ' switch ', ' that ', ' to ', ' type
  of ', ' until ', ' var ', ' void ', ' while ', ' with ';
  $keywords 2 = $keywords; Array_walk ($keywords, ' onLywholewords ');
  Array_walk ($keywords 2, ' Addspan ', ' Blue ');
  $code = Preg_replace ($keywords, $keywords 2, $code);
return $code; }

I hope this article will help you with your PHP programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.