mendoz Posted July 25, 2008 Share Posted July 25, 2008 Freaks! If I have this text [en]news for the site[/en][es]noticias para sitio[/es] how can I filter it into something like this: Array ( [en] => news for the site [es] => noticias para sitio ) What would be the most efficient way? Thanks, Mendoz Quote Link to comment https://forums.phpfreaks.com/topic/116628-how-to-filter-ennewsenesnoticiases/ Share on other sites More sharing options...
MFHJoe Posted July 25, 2008 Share Posted July 25, 2008 <?php $str = "[en]news for the site[/en][es]noticias para sitio[/es]"; $arr = preg_split("(\\[.*?\\])", $str, -1, PREG_SPLIT_NO_EMPTY); $en = $arr[0]; $sp = $arr[1]; echo $sp; ?> I'm sure this isn't the most efficient way, but it's a way, and it works. Sort of. It doesn't take into account what's inside the square brackets (so it could be [hello]text[/hello] and it wouldn't make a difference), but it does separate the two languages. PREG_SPLIT_NO_EMPTY just makes it so preg_split doesn't return any blank values. Quote Link to comment https://forums.phpfreaks.com/topic/116628-how-to-filter-ennewsenesnoticiases/#findComment-599674 Share on other sites More sharing options...
mendoz Posted July 25, 2008 Author Share Posted July 25, 2008 Works, but then you'd lose what's inside the square brackets. Quote Link to comment https://forums.phpfreaks.com/topic/116628-how-to-filter-ennewsenesnoticiases/#findComment-599684 Share on other sites More sharing options...
mendoz Posted July 25, 2008 Author Share Posted July 25, 2008 <?php $str = "[en]news for the site[/en][es]noticias para sitio[/es]"; $arr = preg_split("(\\[.*?\\])", $str,null, PREG_SPLIT_NO_EMPTY); $languages = array('en','es'); $post = array_combine($languages,$arr); ?> gives: Array ( [en] => news for the site [es] => noticias para sitio ) If I want only whats snide the [en][/en], how can I get it? Quote Link to comment https://forums.phpfreaks.com/topic/116628-how-to-filter-ennewsenesnoticiases/#findComment-599695 Share on other sites More sharing options...
MFHJoe Posted July 25, 2008 Share Posted July 25, 2008 I'd just put together a complicated foreach statement to make it work - I didn't know array_combine existed. Good find. Do you just want to get the [en] value from the array you created? That'd just be echo $post["[en]"] Or did I miss the point of that question? Quote Link to comment https://forums.phpfreaks.com/topic/116628-how-to-filter-ennewsenesnoticiases/#findComment-599699 Share on other sites More sharing options...
mendoz Posted July 25, 2008 Author Share Posted July 25, 2008 Sorry, my bad. I'm talking about getting the value from the $str variable, not the array. Quote Link to comment https://forums.phpfreaks.com/topic/116628-how-to-filter-ennewsenesnoticiases/#findComment-599701 Share on other sites More sharing options...
jonsjava Posted July 25, 2008 Share Posted July 25, 2008 this is the answer based on the question asked: <?php $data = "[en]news for the site[/en][es]noticias para sitio[/es]"; $lang = explode("[/en]", $data); $english = str_replace("[en]", "", $lang[0]); $es_1 = str_replace("[es]", "", $lang[1]); $espanol = str_replace("[/es]", "", $es_1); print $english."\n".$espanol; ?> and to get exactly what you asked for: <?php $data = "[en]news for the site[/en][es]noticias para sitio[/es]"; $lang = explode("[/en]", $data); $english = str_replace("[en]", "", $lang[0]); $es_1 = str_replace("[es]", "", $lang[1]); $espanol = str_replace("[/es]", "", $es_1); $langs = array(); $langs['en'] = $english; $langs['es'] = $espanol; print_r($langs); ?> Quote Link to comment https://forums.phpfreaks.com/topic/116628-how-to-filter-ennewsenesnoticiases/#findComment-599718 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.