Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

simple pattern matching 1

Status
Not open for further replies.

kpdvx

Programmer
Dec 1, 2001
87
US
I want to add emoticon functionality to my text posting area. I want to replace :) with in image of a wink :) with a smile, etc. How would i go about doing this?
 
I'd use the preg_replace function with search and replace arrays.

Fill the $search array with all your emoticons. Keep in mind that a lot of the characters used in emoticons are metacharacters and will have to be escaped with the backslash character.

Fill the $replace array elements with all the HTML code necessary to make the image appropriate for each emoticon show up on a web pages.

If you invoke &quot;<input> = preg_replace ($search, $replace, <input>)&quot;, the function will search the input for each element in $search. If it finds it, it will replace it with the respective element in $replace.
 
k, currently i display the text by doing an include:
include('my_text.txt')

How would i parse this included text, or would i have to &quot;include&quot; it a different way?
 
Instead of including it, open the file and parse it.

Or preparse the text before writing it to the file. This might be even better -- that way the parse only has to happen once, regardless of how many times the file is served.
 
explain how i would open it and parse it please. btw, the txt files are uploaded via ftp, so preparsing isnt an option.
 
#initialize your search and replace arrays

$search = array (&quot;:)&quot;, &quot;;)&quot;);
$replace = array (&quot; &quot;
#open the file, read it one line at a time, replace the emoticon shorthands with links, output

$myfilehandle = fopen (&quot;<my file>&quot;, &quot;r&quot;) or die (&quot;Could not open file&quot;);

while ($stringfromfile = fgets ($myfilehandle))
{
$stringfromfile = preg_replace ($search, $replace, $stringfromfile);

print $stringfromfile;
}

fclose ($myfilehandle);
 
Correction:

Replace:

$search = array (&quot;:)&quot;, &quot;;)&quot;);

With

$search = array (&quot;:\)&quot;, &quot;;\)&quot;);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top