Jump to content

preg_match / preg_replace kind of job


rem

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!

Link to comment
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
Link to comment
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.
Link to comment
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 :)

Link to comment
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!
Link to comment
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 :)
Link to comment
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 :)
Link to comment
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.
Link to comment
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? :)
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.