Jump to content

Archived

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

rem

preg_match / preg_replace kind of job

Recommended Posts

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!

Share this post


Link to post
Share on other sites
So do you have one file that contains all the languages and looking to extract each one??

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[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 :)

Share this post


Link to post
Share on other sites
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: en
Text:

Language: es
Text:

Language: fr
Text:

(the actual text, obviously isn't displayed)

Thanks a lot!

Share this post


Link to post
Share on other sites
[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 :)

Share this post


Link to post
Share on other sites
[code]$pattern = '/\[language:([a-z]{2})\](.+?)\[\/language:\1\]/';[/code]

Share this post


Link to post
Share on other sites
[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 :)

Share this post


Link to post
Share on other sites
[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.

Share this post


Link to post
Share on other sites
If you are using MySQL.. why don't you separate into different columns, instead of [language:en]blah?!

Share this post


Link to post
Share on other sites
[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? :)

Share this post


Link to post
Share on other sites

×

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.