WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Mobile Dev World

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : PHP Syntax Highlight - This is a PHP function to highlight PHP code in a string/text.
Categories : PHP, Strings
Yeni Setiawan
Date : Jul 19th 2008
Grade : 3 of 5 (graded 4 times)
Viewed : 2361
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Yeni Setiawan
Action : Grade This Code Example
Tools : My Examples List

 
Like this code?
Show the author your appreciation.
Submit your own code examples 
 

Here is two main functions. First function will call second function and second function will highlight the code and return the result to first function.
<?php
// function to call
function highlight_code($txt){
   
$hasil = preg_replace_callback('{\[code\]((.|\n)+?)\[/code\]}i',"replace_code", $txt);
    return
$hasil;
}

// main function
function replace_code($ketemu){
   
$hasil = trim($ketemu[1], "\n ");
    return
highlight_string($hasil, true);
}
?>


Here is a sample of usage:
<?php
// sample of usage:
$string = "It's how to call PHP info: [code]<?php phpinfo(); ?>[ /code]";
echo
highlight_code($string);
?>



A few functions to create random passwords.
Categories : PHP, Security, Strings
A function that parses a string and replaces http://whatever with a link, and email addresses with a mailto link. This function was designed for the motd package. But will work freely on its own.
Categories : PHP, Strings
Functions that will format a date in either long or short format from a string.
Categories : Date Time, Strings, PHP
Remove URL Parameter Substr Strstr
Categories : PHP, URLs, Strings
Mimic ASP's GetString function with PHP
Categories : PHP, Databases, MySQL, Strings
Using PHP to show text in random colors
Categories : PHP, Beginner Guides, Strings
Allows you to parse a deliniated string and put the individual fields in a SELECT option in a form
Categories : HTML, PHP, Strings
columned txt file to array()?
Categories : Arrays, Strings, Regexps, PHP
Working with files - putting file contents to a string / var
Categories : PHP, Filesystem, Variables, Strings
Pull deliniated text strings into a "SELECT" statement in a form.
Categories : HTML and PHP, PHP, Strings
Using data from a string.
Categories : PHP, Strings, CURL
Calculator for Baroque Violin strings
Categories : Math., PHP, Strings
Simple Email address validation
Categories : Email, PHP, Strings
Look for the *position* of the first occurence of string2 in string1, beginning at position start.
Categories : Complete Programs, PHP, Strings
Compare two texts and display a block of text with the differences between them.
Categories : PHP, PHP Classes, Filesystem, Strings, Arrays
 Jason Hopkins wrote :1821
What exactly is the point in this?  It would be faster and 
simpler to strip the tags, highlight the code, and then 
replace the tags.  'preg_replace_callback' makes this 
script a little to slow to be useful. (On a side 
note...I've learned that it's a little easier/faster to 
scan through each letter looking for the tags and then 
doing what needs to be done withing the tags.