16 Feb, 2010, Zeno wrote in the 1st comment:
Votes: 0
Trying to do a simple regex here and it's not working. Not sure if I'm missing something.

I'm trying to match:
Quote
[translate=en]Hello[/translate]

And pull out "Hello".

I have:
if ( preg_match('/\[translate=([a-z]{2})\](.*)\[\/translate\]/s',$text,$matches) )
return $text."-".$matches[0];

But it doesn't seem to be matching. I've tried a bunch of different ways and still no go.

[EDIT] Hm, that's why. Apparently I need to be matching:
Quote
[translate=en:9jynfh9r]Hello[/translate:9jynfh9r]


Do I need to escape the colon? I don't think it's pulling out Hello (matches[0])…
16 Feb, 2010, David Haley wrote in the 2nd comment:
Votes: 0
Well you're only checking for two characters after the equal sign. It looks like you need to check for more. Try something like:

/\[translate=([a-z]{2})(:\w*)?\](.*)\[\/translate\]/s

or if you want to be more loose about it:
/\[translate=([a-z0-9A-Z:]*\](.*)\[\/translate\]/s
16 Feb, 2010, Zeno wrote in the 3rd comment:
Votes: 0
Yeah, didn't realize I was matching the wrong thing. Think I'm all set now.

Another related question. I need to replace "Hello", and I've been using preg_match and preg_replace to do this. I use match to get "Hello" which gets looked up in the database and stored into a string. I then use replace to replace "Hello" with what came from the database.

Is there a better way to do this, say with just preg_replace?
16 Feb, 2010, David Haley wrote in the 4th comment:
Votes: 0
In Lua you can give a custom function for how to handle substitutions, which can avoid the two scans of the string. I don't remember if PHP has a similar function, but it might. Another option would be to match all three components ("[translate=…]", "Hello", "[/translate]") and then construct a new string by concatenating the first and third match with the DB's result in the middle.
16 Feb, 2010, kiasyn wrote in the 5th comment:
Votes: 0
17 Feb, 2010, Zeno wrote in the 6th comment:
Votes: 0
Thanks. I think I've got it working and everything.

Here are my results (based on this feature from FFXI).
17 Feb, 2010, Kayle wrote in the 7th comment:
Votes: 0
Hahaha. Squiggles. Makes me want to play FF11 for a bit.
0.0/7