Regular expression help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jeremyc
    New Member
    • Feb 2012
    • 2

    Regular expression help

    Hi guys

    I have a case where data is getting stored in the database as: <!--:en-->Energy<!--:--><!--:fr-->Energies<!--:-->

    Now in a given page, I have en or fr, and based on that I need to get the corresponding value.

    For example, if I have
    Code:
    $title	= '<!--:en-->Energy<!--:--><!--:fr-->Energies<!--:-->';
    $lng 	= 'fr';
    $title 	= preg_replace('/<!--:'.$lng.'-->(.*?)<!--:-->/i', '$1', $title);
    I want to get "Energies" by echoing $title.

    Any idea ?

    PS. The preg_replace is just there for an example, I know the code is not working.
    Last edited by Dormilich; Feb 27 '12, 10:30 AM. Reason: spelling
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    I have a case where data is getting stored in the database as: <!--:en-->Energy<!--:--><!--:fr-->Energies<!--:-->
    from the overall point-of-view this is the main problem. a DB column should not contain aggregated data. it would be more sensible to have a field for the word and another field for the language, so you can switch between languages by making the appropriate query.


    for the quick'n'dirty fix, you need the non-greedy modifier in your RegExp.

    Comment

    • jeremyc
      New Member
      • Feb 2012
      • 2

      #3
      Thanks Dormilich. Yes, this is not a good DB design, but it's the qTranslate plugin of Wordpress, that allows to have a multilingual website.

      So basically I need a quick'n'dirty fix. However, how to rewrite that code with a non-greedy modifier Dormilich?

      Thanks for your reply again, non-greedy modifier

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        Yes, this is not a good DB design, but it's the qTranslate plugin of Wordpress, that allows to have a multilingual website.
        if possible, use another plugin.


        you should look up regular expressions (ref.), even I have to look up such things.

        Comment

        Working...