Jump to content

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


mendoz

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.