rem Posted October 19, 2006 Share Posted October 19, 2006 Hi,I'm trying to develop a plugin for my personal CMS. I need this to recognize any tags like [language:en] text here [/language:en] so I can have several languages (separated by this) within the same post. I managed to make it recognize the tags using regex but can't output within a variable or an array the actual language(s) (en, es, fr, or whatever) ... This is my code:[code]<?php$text = '[language:en] Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam [/language:en]';$rx = '/\[language:([a-z]{2})\]|\[\/language:([a-z]{2})\]/';$text2 = preg_replace($rx, '', $text);echo $text2;?>[/code]Any suggestions are very appreciated.Thanks a lot! Quote Link to comment Share on other sites More sharing options...
craygo Posted October 19, 2006 Share Posted October 19, 2006 So do you have one file that contains all the languages and looking to extract each one?? Quote Link to comment Share on other sites More sharing options...
rem Posted October 19, 2006 Author Share Posted October 19, 2006 I have mysql entries as posts. I want to make possible having within the same posts, multiple languages separated by [language:po][/language:po] tags. When (for example) es or fr or en language link is clicked, only that specific text, from between the specific language tags to be displayed.ex: index.php?pg=my_page.php&lang=en - > when lang=en, need to extract only the [language:en] this is my english text [/language:en] tags from the "my_page.php" post. Any ideas?Thank you Quote Link to comment Share on other sites More sharing options...
obsidian Posted October 19, 2006 Share Posted October 19, 2006 try this:[code]<?php$pattern = '|\[language:([a-z]{2})\](.+?)\[/language:$1\]|i';preg_match_all($pattern, $String, $matches, PREG_SET_ORDER);for ($i = 0; $i < count($matches); $i++) { echo "<p>Language: " . $matches[$i][1] . "<br />\n"; echo "Text:<br />" . $matches[$i][2] . "</p>\n";}?>[/code]preg_match_all() really is going to be the only way to pull all the submatches you're after. then, you can use the results to parse as you see fit. Quote Link to comment Share on other sites More sharing options...
rem Posted October 19, 2006 Author Share Posted October 19, 2006 [quote author=obsidian link=topic=112039.msg454558#msg454558 date=1161282331]try this:[code]<?php$pattern = '|\[language:([a-z]{2})\](.+?)\[/language:$1\]|i';preg_match_all($pattern, $String, $matches, PREG_SET_ORDER);for ($i = 0; $i < count($matches); $i++) { echo "<p>Language: " . $matches[$i][1] . "<br />\n"; echo "Text:<br />" . $matches[$i][2] . "</p>\n";}?>[/code]preg_match_all() really is going to be the only way to pull all the submatches you're after. then, you can use the results to parse as you see fit.[/quote]Thank you, that's a wonderful idea... even though, I guess something's wrong with the pattern, cause I don't have any results. If I replace your pattern with mine, the language gets extracted but not the text, and also, each is displayed twice... So kinda messed up. I know my pattern is not quite great, cause my regex skills are pretty sucky :) Quote Link to comment Share on other sites More sharing options...
rem Posted October 19, 2006 Author Share Posted October 19, 2006 I used obsidian's example (thank you) and played a bit with the pattern but didn't went to far, and few hours later, here I am , giving up... :( Can you help please? My code looks like this now:[code]<?php$string = '[language:en] Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam [/language:en]<br />[language:es] Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam [/language:es]<br />[language:fr] Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam [/language:fr]<br />';$pattern = '/\[language:([a-z]{2})\]|\[\/language:$1\]/';preg_match_all($pattern, $string, $matches, PREG_SET_ORDER);for ($i = 0; $i < count($matches); $i++) { echo "<p>Language: " . $matches[$i][1] . "<br />\n"; echo "Text:<br />" . $matches[$i][2] . "</p>\n";}?>[/code]and all I managed to get from it is:Language: enText:Language: esText:Language: frText:(the actual text, obviously isn't displayed)Thanks a lot! Quote Link to comment Share on other sites More sharing options...
Jenk Posted October 19, 2006 Share Posted October 19, 2006 Why not use XML? Quote Link to comment Share on other sites More sharing options...
rem Posted October 19, 2006 Author Share Posted October 19, 2006 [quote author=Jenk link=topic=112039.msg454711#msg454711 date=1161299792]Why not use XML?[/quote]Not quite sure, how... would that be more helpful than PHP? I think I'd rather stick with what I already got. If I'll manage to solve the regex mistery somehow, it would probably work too :) Quote Link to comment Share on other sites More sharing options...
effigy Posted October 19, 2006 Share Posted October 19, 2006 [code]$pattern = '/\[language:([a-z]{2})\](.+?)\[\/language:\1\]/';[/code] Quote Link to comment Share on other sites More sharing options...
rem Posted October 19, 2006 Author Share Posted October 19, 2006 [quote author=effigy link=topic=112039.msg454718#msg454718 date=1161300857][code]$pattern = '/\[language:([a-z]{2})\](.+?)\[\/language:\1\]/';[/code][/quote]WOW!!! Thanks a mil obsidian and effigy, you guys really ROCK! My stuff is working now!! WOW, that's so sweeet :) Quote Link to comment Share on other sites More sharing options...
Jenk Posted October 19, 2006 Share Posted October 19, 2006 I meant use XML to store the data. Quote Link to comment Share on other sites More sharing options...
rem Posted October 20, 2006 Author Share Posted October 20, 2006 [quote author=Jenk link=topic=112039.msg454721#msg454721 date=1161301880]I meant use XML to store the data.[/quote]Oh, yes, that's a pretty good idea, but I'm already using MySQL to hold my posts and the above seemd to be the easier solution to deal with it... Thank you. Quote Link to comment Share on other sites More sharing options...
Jenk Posted October 20, 2006 Share Posted October 20, 2006 If you are using MySQL.. why don't you separate into different columns, instead of [language:en]blah?! Quote Link to comment Share on other sites More sharing options...
rem Posted October 20, 2006 Author Share Posted October 20, 2006 [quote author=Jenk link=topic=112039.msg454834#msg454834 date=1161334120]If you are using MySQL.. why don't you separate into different columns, instead of [language:en]blah?![/quote]I thought about that but users will have to choose their language from a dropdown, then insert a new post for each language and I want to keep it simple within the same post, everything in the same editor window. This is an ideea I got from the Polyglot WordPress plugin and I found it to be very practicable. I'm developing this for people who needs their websites content translated into multiple languages and besides that, I think it's pretty neat, eh? :) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.