Jump to content


Photo

preg_match / preg_replace kind of job


  • Please log in to reply
13 replies to this topic

#1 rem

rem
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 19 October 2006 - 06:03 PM

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:

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

Any suggestions are very appreciated.

Thanks a lot!



#2 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 19 October 2006 - 06:14 PM

So do you have one file that contains all the languages and looking to extract each one??

#3 rem

rem
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 19 October 2006 - 06:24 PM

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

#4 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 19 October 2006 - 06:25 PM

try this:
<?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";
}
?>

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.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#5 rem

rem
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 19 October 2006 - 06:38 PM

try this:

<?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";
}
?>

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.


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 :)



#6 rem

rem
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 19 October 2006 - 11:12 PM

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:

<?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";
}
?>

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!

#7 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 19 October 2006 - 11:16 PM

Why not use XML?

#8 rem

rem
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 19 October 2006 - 11:28 PM

Why not use XML?

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 :)

#9 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 19 October 2006 - 11:34 PM

$pattern = '/\[language:([a-z]{2})\](.+?)\[\/language:\1\]/';

Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#10 rem

rem
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 19 October 2006 - 11:38 PM

$pattern = '/\[language:([a-z]{2})\](.+?)\[\/language:\1\]/';


WOW!!!
Thanks a mil obsidian and effigy, you guys really ROCK! My stuff is working now!! WOW, that's so sweeet :)

#11 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 19 October 2006 - 11:51 PM

I meant use XML to store the data.

#12 rem

rem
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 20 October 2006 - 12:08 AM

I meant use XML to store the data.


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.

#13 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 20 October 2006 - 08:48 AM

If you are using MySQL.. why don't you separate into different columns, instead of [language:en]blah?!


#14 rem

rem
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 20 October 2006 - 09:52 AM

If you are using MySQL.. why don't you separate into different columns, instead of [language:en]blah?!


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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users