08-06-03, 07:52 PM
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 ;}
08-07-03, 01:32 AM
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.:
pretty easy...
cheers
__________________
TAGword, ads the way you always wanted.
<a href="http://tagword.com">tagword.com</a>
08-07-03, 01:34 AM
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
08-07-03, 09:37 AM
Warrior Soul
Join Date: Aug 2003
Location: San Pedro (B) Argentina
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
08-07-03, 04:43 PM
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>
08-08-03, 02:40 AM
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?
08-08-03, 04:45 PM
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!
08-08-03, 06:56 PM
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...
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off