Jump to content

how to Filter [en]News[/en][es]Noticias[/es]


mendoz

Recommended Posts

<?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.

<?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?

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);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.