Banner

Sponsor

Login


Welcome Back!
Guest
Guest

Register

Lost your password?

46 users online



In Depth Tutorials [May 30, 2004]

In Depth Tutorials [May 30, 2004]

Currently viewing this thread: 1 (0 members and 1 guests)


Thread closed

Closed by Noel on August 24th, 2005 03:41 PM

Not discussion.

Page 4 out of 4
BigToach

BigToach

Neversidian
Status: Offline!

Changing Text To Smilies.

Changing standard text into Smiley Images
Just for reference, I will be referring to the word "string" and "array" a lot in this tutorial. . There is more information on strings, arrays, and different types of variables Here and at PHP.net

This Tutorial uses the same idea as the BBCode and the Template tutorials. The basic idea behind turning plaintext smilies into images is this;
You take some text that you have stored in either files or mysql,
You tell php to find certain patterns in the text,
and then you tell php to replace those patterns with the html for an image.
There are many, many, many ways to do this, but I will be showing you a very simple way of doing it.
I will give you the code first then i will go throgh and explain it peice by peice.

PHP:

<?php

$smiles 
= array(
':B'=>'<img src="buckteeth.gif" alt="" />',
'mad'=>'<img src="mad.gif" alt="" />');
$plain_text 'I am more mad than an amish electrician :B';
$smiles_to_find array_keys($smiles);
$images_to_replace array_values($smiles);
$smile_text str_replace($smiles_to_find,$images_to_replace,$plain_text);
echo 
$smile_text;

?>


Now lets break it down.
First I started by making an array of smiles. The smile that you want to find in the text is the key of one element of the array. You could make it anything that you wanted. The smile is followed by the img html that points to where the image is.
you can think of it like this you want to turn ;B into this <img src="buckteeth.gif" alt="" /> or
:B => <img src="buckteeth.gif" alt="" />
you can put as many smilies and images into this array as long as you don't put two of the same.
Next we defined our text. You could pull the text out of mysql, a file(file_get_contents()), or just use variables like i did, but it doesn't matter which method you use.
Next was this line.

PHP:

<?php

$smiles_to_find 
array_keys($smiles);

?>


This line basically just took all of the smiles to find and put them in their own array. In our case if i were to print_r() the $smiles_to_find array it would look like this:

Code:

Array
(
[0] => :B
[1] => mad
)


Next I did a similar thing on the next line as well.
I took all of the images to find in the $smiles array and put them in their own array.
Again in our case if i were to print_r() the $images_to_replace array it would look like this:

Code:

Array
(
[0] => <img src="buckteeth.gif" alt="" />
[1] => <img src="mad.gif" alt="" />
)


Now we have two arrays, the $smiles_to_find array and the $images_to_replace array. See how each smilie's key corrosponds to the images key. That will allow us to replace all of the $smiles_to_find with their corresponding images in $images_to_replace.
Now we will actually replace them. To do so we use the php function str_replace().
str_replace has 3 arguements. (str_replace( FIND , REPLACE , STRING ) )
It function works like this:
It looks in STRING for FIND and if it is there, it replaces it with REPLACE.
in our situation we will use arrays for FIND and REPLACE. so when it finds any one FIND it will replace it with the corresponding REPLACE.
when it is all done searching the text it returns the result to a new variable.
so our actual php will be like I posted earlier:

PHP:

<?php

$smile_text 
str_replace($smiles_to_find,$images_to_replace,$plain_text);

?>


all that we have left to do is echo the finished product

PHP:

<?php

echo $smilie_text;

?>


Similar to my post on nl2br() This does alter the text so it is best to apply this to the text on display instead of before saving it.
ALSO Like i said ealier, you can add as many different smiles to this as you want, and you could tweak it to make it catch badwords as well. just put a badword for the smile to find(if you want to remove small words like ***, but still allow for assumption make the smile be ' *** ' w/o the quotes) and instead of placing an image there you can just put *** or other stuff.

___________________

Neverside Development Director
PHP Snippets
BigToach.com - IT WORKS, TOACHY!

Last edited by Noel, May 7th, 2005 10:37 PM (Edited 1 times)

Phil

Phil

with Mr. Jones
Status: Offline!

Advanced Highlighter

I was messing around, did this - it gets rid of all those font tags, everything is controlled by CSS. An improvement to it might be a caching tool of sorts. The only lacking in this is the fact that when you c/p, if line numbers are on, they copy as well. I suppose this could be resolved by using a table and putting line numbers in their own cell, so do that if you want.

You can use the function like this

PHP:

<?php

highlight
($code$php_adjust=true$line_numbers=true

?>

If $php_adjust is true, it adds <?php if it cant find any. $line_numbers determines if line numbers are enabled or not. The function returns the code, it doesn't print it.

It highlights, switches from color-fonts to span-classes, and makes links for functions.

Heres the code:

PHP:

<?php

function highlight($code$php_adjust=true$line_numbers=true)
{
    
// Adjusts for missing <?php tags if non are found
    
$code = ($php_adjust && !strstr($code'<?')) ? '<?php' "\n" $code "\n" '?>' $code;

    
// Highlight
    
$code highlight_string($codetrue);

    
// Make function links, borrowed from pastebin source
        
$code=preg_replace(
            
//match a highlighted keyword
            
'{([\w_]+)(\s*</font>)'.
            
//followed by a bracket
            
'(\s*<font\s+color="'.ini_get('highlight.keyword').'">\s*\()}m',
            
//and replace with manual hyperlink
            
'<a class="php_code_link" title="View manual page for $1" href="http://www.php.net/manual-lookup.php?lang=en&amp;pattern=$1">$1</a>$2$3'$code);
    
// Make switch to CSS happy
    
$colors[ini_get('highlight.bg')] = 'php_background';
    
$colors[ini_get('highlight.comment')] = 'php_comment';
    
$colors[ini_get('highlight.default')] = 'php_default';
    
$colors[ini_get('highlight.html')] = 'php_html';
    
$colors[ini_get('highlight.keyword')] = 'php_keyword';
    
$colors[ini_get('highlight.string')] = 'php_string';
    
// Actually switch
    
foreach($colors as $color=>$class)
    {
        
$code str_replace('<font color="' $color '">''<span class="' $class '">'$code);
    }
    
// And add line numbers
    
$lines explode('<br />'str_replace('</font>''</span>'$code));
    if(
$line_numbers)
    {
        unset(
$code);
        
$final '';
        foreach(
$lines as $line_number=>$line)
        {
            
$final .= '<span class="php_line_numbers">' . ($line_number 1) . '</span>' $line '<br />';
        }
        return 
$final;
    }
    else
    {
        return 
$code;
    }
}

?>
<html>
    <head>
        <title>highlighter</title>
        <style type="text/css" media="screen">
            .code
            {
                border: 1px solid black;
                padding: .5em;
                font-family: courier, system, monospace;
            }
            .php_line_numbers
            {
                margin-right: 12px;
                color: grey;
            }
            .php_code_link
            {
                color: red;
            }
            .php_background
            {
                background-color: #E2EDFD;
            }
            .php_comment
            {
                color: #FF8000;
            }
            .php_default
            {
                color: #0000BB;
            }
            .php_html
            {
                color: #3E4F67;
            }
            .php_keyword
            {
                color:     #007700;
            }
            .php_string
            {
                color: #DD0000;
            }
        </style>
    </head>
    <body>
        <div class="code php_background">
            <?php print highlight(file_get_contents('hl.php')); ?>
        </div>
    </body>
</html>

And this is an example in action:

http://www.aesthetic-theory.com/hl.php

Hope it helps some of you!

___________________

http://www.philbrodeur.com - Expert PHP Development and Tutorials

Last edited by Noel, May 7th, 2005 10:38 PM (Edited 1 times)

BigToach

BigToach

Neversidian
Status: Offline!

Magic Quotes: Magically Annoying

Magic Quotes: Magically Annoying
Many beginner (and experienced) PHP programmers are not aware of one of the most annoying "features" of PHP: Magic Quotes. Not knowing about this evil and being unprepared against it will leave your scripts buggy and host dependent. What are Magic Quotes?

Magic Quotes was intentionally a nifty little PHP feature that would essentially run addslashes() on all incoming GET, POST, and COOKIE variables. The reason for this is to keep quotes and backslashes in database queries from screwing up your SQL. This was a good idea, except for one problem: it was optionable. This means that Magic Quotes may not be running on all servers, so using Magic Quotes means that switching servers is a pain.

Luckily, this can be solved with a simple block of code:

PHP:

<?php

function array_stripslashes($array) {
  foreach (
$array as $key => $value) {
    
$array[$key] = (is_array($value) ? recursive_stripslashes($value) : stripslashes($value));
  }
  return 
$array;
}

set_magic_quotes_runtime(0);
$_GET array_stripslashes($_GET);
$_POST array_stripslashes($_POST);
$_COOKIE array_stripslashes($_COOKIE);

?>


We use set_magic_quotes_runtime() to disable a relative of magic_quotes, then use the stripMagicQuotes function to remove the magic quotes from the GET, POST and COOKIE variables.

The variable is passed by reference so the function is able to directly modify the inputted variable. The function first checks if Magic Quotes are on; if they aren't don't bother to fix them, so return. Next we loop through the variable checking if each value is an array or not. If it is an array, run the function again on the child array. This is a recursive function. If it isn't an array, we edit the inputted array and use stripslashes() to undo the terrible deed Magic Quotes have done. After that, we simply run the function on all of the infected variables: $_GET, $_POST, and $_COOKIE.

If the above explanation confused you, don't worry. All you need to do to fix this problem is add the above code at the top of your pages. Whenever you need a variable to be database-safe, use addslashes() to manually error-proof it.

Happy scripting!
Post originally written by Radley

___________________

Neverside Development Director
PHP Snippets
BigToach.com - IT WORKS, TOACHY!

Last edited by Rad, November 2nd, 2005 03:51 AM (Edited 2 times)

Page 4 out of 4
Quick Jump:

Main Navigation


Site & Graphic Design by Aeon Tan
Developed by Jeremie Pelletier & Scott Roach


NeverAPI generated this page in 0.0232 seconds.