Current location: Hot Scripts Forums » Programming Languages » PHP » Optimizing functions


Optimizing functions

Reply
  #1 (permalink)  
Old 08-06-03, 07:52 PM
amidamaru's Avatar
amidamaru amidamaru is offline
Warrior Soul
 
Join Date: Aug 2003
Location: San Pedro (B) Argentina
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Optimizing functions

There is a way of making this function smaller and faster?
PHP Code:

function codigo($texto){

// CODIGO PARA EL FORMATO DEL TEXTO
$texto str_replace("[br]","<br>",$texto);
$texto str_replace("[hr]","<hr>",$texto);
$texto str_replace("[n]","<b>",$texto);            $texto str_replace("[/n]","</b>",$texto);
$texto str_replace("[k]","<i>",$texto);            $texto str_replace("[/k]","</i>",$texto);
$texto str_replace("[s]","<u>",$texto);            $texto str_replace("[/s]","</u>",$texto);
$texto str_replace("[0]","<font color='black'>",$texto);    $texto str_replace("[/0]","</font>",$texto);
$texto str_replace("[1]","<font color='white'>",$texto);    $texto str_replace("[/1]","</font>",$texto);
$texto str_replace("[2]","<font color='blue'>",$texto);    $texto str_replace("[/2]","</font>",$texto);
$texto str_replace("[3]","<font color='red'>",$texto);    $texto str_replace("[/3]","</font>",$texto);
$texto str_replace("[4]","<font color='green'>",$texto);    $texto str_replace("[/4]","</font>",$texto);
$texto str_replace("[5]","<font color='yellow'>",$texto);    $texto str_replace("[/5]","</font>",$texto);
$texto str_replace("[6]","<font color='deeppink'>",$texto);    $texto str_replace("[/6]","</font>",$texto);
$texto str_replace("[7]","<font color='orange'>",$texto);    $texto str_replace("[/7]","</font>",$texto);
$texto str_replace("[8]","<font color='brown'>",$texto);    $texto str_replace("[/8]","</font>",$texto);
$texto str_replace("[9]","<font color='silver'>",$texto);    $texto str_replace("[/9]","</font>",$texto);
$texto str_replace("[*]","<li>",$texto);            $texto str_replace("[/*]","</li>",$texto);
// CARGAR SONRISAS
$texto str_replace("[oo]","<img src='img/[oo].gif'>",$texto);
$texto str_replace("[xx]","<img src='img/[xx].gif'>",$texto);
$texto str_replace("[òó]","<img src='img/[enojado].gif'>",$texto);
$texto str_replace("[óò]","<img src='img/[triste].gif'>",$texto);
$texto str_replace(":D","<img src='img/D.gif'>",$texto);
$texto str_replace(":p","<img src='img/P.gif'>",$texto);
$texto str_replace(":P","<img src='img/P.gif'>",$texto);
// CAMBIAR E-MAILS Y LINKS
$texto eregi_replace("http://","",$texto);
$texto eregi_replace("www","http://www",$texto);
$texto eregi_replace("ftp://","ftp://",$texto);
$texto eregi_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]""<a href='\\0' target='_blank'>link</a>"$texto);
$texto eregi_replace("([a-zA-Z0-9_-]+)@([a-zA-Z0-9\._-]+)(\.[a-zA-Z]+)""<a href='mailto:\\1@\\2\\3'>email</a>"$texto);
return 
$texto;} 
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 08-07-03, 01:32 AM
joeldg joeldg is offline
Newbie Coder
 
Join Date: Jul 2003
Location: New Orleans
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
yes there is...

First make an array of your replacements. would most likely be coming from a DB
Then loop through them..
i.e.:
PHP Code:

$repl = array(

    
"[br]" => "<br>",
    
"[hr]" => "<hr>"
); // etc....

function codigo($texto){
global 
$repl;
   while(list(
$key,$val)=each($repl)){
    
$texto str_replace($key,$val,$texto);
   }
//wend

}//end function
#...  etc rest of function 

pretty easy...
cheers
__________________
TAGword, ads the way you always wanted.
<a href="http://tagword.com">tagword.com</a>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 08-07-03, 01:34 AM
Archbob Archbob is offline
Newbie Coder
 
Join Date: Jul 2003
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
Can't you just make an array of everything you want to replace and call str_replace once?
__________________
Master Chipmunk and programmer of cheap scripts.
Chipmunk Scripts -- Free GPL scripts
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 08-07-03, 09:37 AM
amidamaru's Avatar
amidamaru amidamaru is offline
Warrior Soul
 
Join Date: Aug 2003
Location: San Pedro (B) Argentina
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Great ideas
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 08-07-03, 04:43 PM
joeldg joeldg is offline
Newbie Coder
 
Join Date: Jul 2003
Location: New Orleans
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Ahhh just looked at php.net and you are correct sir!

In PHP 4.0.5 and later, every parameter to str_replace() can be an array.

Yes.. just arrays
__________________
TAGword, ads the way you always wanted.
<a href="http://tagword.com">tagword.com</a>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 08-08-03, 02:40 AM
nd2 nd2 is offline
Wannabe Coder
 
Join Date: Jun 2003
Posts: 128
Thanks: 0
Thanked 0 Times in 0 Posts
$thearr = array(
"\[br](.*?)\[/br]" => "<br>\\1</br>",
"\(.*?)\" => "<u>\\1</u>" );

foreach (array_keys($thearr) as $i) {
$input = preg_replace( "#$i#si", $thearr[$i], $input );
}

// i havent tested it, but it whould cut your replaces by 1/2, but you will need to use the preg_replace function which is slower, maybe try timing them to see which is faster?
__________________
IonCMS (Coming Soon.)
http://ioncms.com
--
Ncaster (Free php/mysql cms)
http://ncaster.cjb.net
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 08-08-03, 04:45 PM
RocketPack.net RocketPack.net is offline
Newbie Coder
 
Join Date: Aug 2003
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Actually the preg functions are much faster than the ereg functions, as noted here: http://www.php.net/ereg
"Note: preg_match(), which uses a Perl-compatible regular expression syntax, is often a faster alternative to ereg()."

I thought that this might interest you-
http://www.php.net/str_replace
"In PHP 4.0.5 and later, every parameter to str_replace() can be an array."

Meaning, if you make an array of finds, an array of replaces, you would only have to make 1 call to str_replace.
ie:
$find = array('[br]', '[hr]', '[oo]', '[xx]', '[òó]');
$replace = array('<br>', '<hr>', "<img src='img/[oo].gif'>", "<img src='img/[xx].gif'>", "<img src='img/[enojado].gif'>");
$texto = str_replace($find, $replace, $texto);

Good luck!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #8 (permalink)  
Old 08-08-03, 06:56 PM
amidamaru's Avatar
amidamaru amidamaru is offline
Warrior Soul
 
Join Date: Aug 2003
Location: San Pedro (B) Argentina
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
my problem is solved... an new stuff i have learned.. thank you! all
__________________
My english suks... i know sorry
i speak spanish...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Object Oriented Programming Stefan PHP 29 12-30-03 12:22 PM
VBscript... math functions and stuff that hurts my brain. Mister B. ASP 0 08-31-03 02:04 PM
Array passing between functions alfreds PHP 1 08-25-03 06:29 AM
User Functions DA Master PHP 4 06-15-03 06:00 AM
Need more functions for my wysiwyg editor klausre PHP 3 06-14-03 12:35 PM


All times are GMT -5. The time now is 08:48 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.