Click to See Complete Forum and Search --> : [RESOLVED] Is this to time consuming?
Kyleva2204
03-22-2006, 10:13 PM
hello, I just wanna know if there is another way to do this script.. like if theres a function built into PHP to achieve the same results.. cuz Im afraid that once my entire script is said and done, it will take far to long to compile the page...
here is the script im using now:
<?php
// dynamics //
$text = explode("%=>", $text);
foreach ($text as $text_a){
// LANGUAGE DYNAMICS //
///////////////////////
ereg("<%lang\(([^\"]*)\)", $text_a, $text_regs);
$text_a = preg_replace("|<%lang\((.*)\)|Ui", lang($text_regs[1],0) ,$text_a);
$text_n .= $text_a;
}?>
Thanks in advance!
Kyle
NogDog
03-23-2006, 01:13 AM
My guess is that it could all be done with one preg_replace() call, but there's not enough info for me to really know: I'd need to know what $text looks like initially, and what exacty you want to do.
Kyleva2204
03-23-2006, 12:46 PM
The text in $text is plain HTML text from an HTML file.. and what im doing is serching for the string %lang(1) or any number inside the (), and replace it with the text from a function lang() and place the number inside the () inside of the function.... sooo yeah
Kyleva2204
03-23-2006, 09:59 PM
anyone... :-\
sorry for DP! lol.. but I reallllly need help heh
NogDog
03-23-2006, 11:13 PM
$text = preg_replace('/(?<=%lang\()\d+(?=\))/i', lang($text_regs[1],0), $text);
Kyleva2204
03-23-2006, 11:46 PM
.... don't I still need to find all the occurences and grab the data and put them in an array ($text_regs)...? or, will thye be there still? or what yo?.. Because in my code I have now, I have the ereg to get all the numbers and all..
NogDog
03-24-2006, 01:20 AM
$text = preg_replace('/(?<=%lang\()(\d+)(?=\))/ie', "lang(\\1, 0)", $text);
Kyleva2204
03-24-2006, 01:47 AM
wait- are you serious? that will actually work!? I gotta see thizzz..
Edit:
yeahhh I didn't think soh.. :-\ it outputs the following: lang(3,0) instead of the text the fucntion outputs.. any other ideas? :-\..
NogDog
03-24-2006, 04:34 AM
I guess I'm not following exactly what you want replaced with what. A small example of before and after text would be extremely useful (sort of "a picture is worth a thousand words" thing). I think I may have been over-complicating things. Is this, perhaps, all you're looking for?:
$text = preg_replace('/%lang\((\d+)\)/ie', "lang(\\1, 0)", $text);
Note: the "e" after the final "/" of the regexp is critical, telling the function to execute the 2nd arg as a PHP command. (The "i" before it is optional, and can be left out if you don't want the regexp to be case-insensitive, but that "e" must be there.)
bokeh
03-24-2006, 04:50 AM
If you want to use the output of a function to make the replacement based on the captured text you need to use preg_replace_callback() (http://www.php.net/preg_replace_callback).
I'm not really sure what you are up to but if you are trying to implement a multilingal site with this code, it sure is a bizarre way to do it.
Kyleva2204
03-24-2006, 08:19 AM
If you want to use the output of a function to make the replacement based on the captured text you need to use preg_replace_callback() (http://www.php.net/preg_replace_callback).
I'm not really sure what you are up to but if you are trying to implement a multilingal site with this code, it sure is a bizarre way to do it.
welll---- I have my reasons for making it this way.. However, how would it normally be done? /: )
----
annd yeah.. heh.. the E thingy worked O:-)... Thanks.. meh.. soo that did what I wanted it to :D Gracias!
bokeh
03-24-2006, 08:48 AM
However, how would it normally be done? /: )Well these days with SQL I guess. You would have a table with all your different blocks of text on one axis and your languages on the other. After your logic to find out the language to use it is simply a matter of adding $language to your SQL query.SELECT $language
FROM texts
On my site (http://costablancatranslations.com/) I used the file system for the bilingualism. And on this page (http://bokehman.com/calendar) I applied the same logic to four languages.
Kyleva2204
03-24-2006, 12:40 PM
Well these days with SQL I guess. You would have a table with all your different blocks of text on one axis and your languages on the other. After your logic to find out the language to use it is simply a matter of adding $language to your SQL query.SELECT $language
FROM texts
On my site (http://costablancatranslations.com/) I used the file system for the bilingualism. And on this page (http://bokehman.com/calendar) I applied the same logic to four languages.
Okidokie- lol... I'm doing exactly that yo, Just adding onto the process.. ^_^