RegEx with preg_replace

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sebastian Araya

    RegEx with preg_replace

    Hello,

    I have a string like this:

    var1: value1...valueI var2: value1...valueJ ... varN:
    value1...valueK

    this is an example:

    breakfast: coffee eggs lunch: sandwich apple dinner: chicken wine
    cake

    I'm trying to use preg_replace to change "lunch:" values to a code
    number, in order to get in the example:

    breakfast: coffee eggs lunch: 0503 dinner: chicken wine cake

    keeping the rest of variables and values unchanged.

    Take in account, that I'm only sure that I have the "lunch:" word
    delimitation (and its values), I don't know if exists "breakfast: " or
    "dinner:"; but I'm sure that if the variable exists, then the values
    exists too.

    Thanks,

    Sebastian.
  • Pedro Graca

    #2
    Re: RegEx with preg_replace

    Sebastian Araya wrote:[color=blue]
    > this is an example:
    >
    > breakfast: coffee eggs lunch: sandwich apple dinner: chicken wine
    > cake
    >
    > I'm trying to use preg_replace to change "lunch:" values to a code
    > number, in order to get in the example:
    >
    > breakfast: coffee eggs lunch: 0503 dinner: chicken wine cake
    >
    > keeping the rest of variables and values unchanged.[/color]

    Not sure if preg_* is the way to go ... but, what the hell :-)

    <?php
    $foods['00'] = 'coffee';
    $foods['01'] = 'wine';
    $foods['02'] = 'cake';
    $foods['03'] = 'apple';
    $foods['04'] = 'eggs';
    $foods['05'] = 'sandwich';
    $foods['06'] = 'chicken';
    $line = 'breakfast: coffee eggs lunch: sandwich apple dinner: chicken wine cake';

    preg_match('/^(breakfast: .* )?lunch: (.*)( dinner:.*)?$/U', $line, $meal);
    $liunch = explode(' ', $meal[2]);

    echo $meal[1], 'lunch: ';
    foreach ($lunch as $foodname) echo array_search($f oodname, $foods);
    echo $meal[3], "\n";
    ?>


    Output of this script is:
    breakfast: coffee eggs lunch: 0503 dinner: chicken wine cake
    --
    --= my mail box only accepts =--
    --= Content-Type: text/plain =--
    --= Size below 10001 bytes =--

    Comment

    • Chung Leong

      #3
      Re: RegEx with preg_replace

      Use preg_replace_ca llback(). In the callback function, parse the list of
      lunch items and look up the corresponding code for each.
      In the regexp you need to use preg's lookbehind feature, to look for the
      string "lunch:" but keeping it out of the match. The following snippet
      should yield the right result:

      $s = "breakfast: coffee eggs lunch: sandwich apple dinner: chicken wine
      cake";

      $food_ids = array( 'coffee' => '00', 'wine' => '01', 'cake' => '02',
      'apple' => '03', 'eggs' => '04', 'sandwich' => '05', 'chicken' => '06' );

      function food_to_code($m atches) {
      global $food_ids;
      $foods = preg_split('/\\s+/', $matches[0]);
      $codes = "";
      foreach($foods as $food) {
      $code = $food_ids[$food];
      if($code) {
      $codes .= $code;
      }
      }
      return "$codes ";
      }

      echo preg_replace_ca llback('/(?<=lunch:\\s)( ?:\\w+(?:\\s|$) )+/',
      'food_to_code', $s);

      Uzytkownik "Sebastian Araya" <araya.s@numisy s.com.ar> napisal w wiadomosci
      news:678aed25.0 312310920.d56c5 2f@posting.goog le.com...[color=blue]
      > Hello,
      >
      > I have a string like this:
      >
      > var1: value1...valueI var2: value1...valueJ ... varN:
      > value1...valueK
      >
      > this is an example:
      >
      > breakfast: coffee eggs lunch: sandwich apple dinner: chicken wine
      > cake
      >
      > I'm trying to use preg_replace to change "lunch:" values to a code
      > number, in order to get in the example:
      >
      > breakfast: coffee eggs lunch: 0503 dinner: chicken wine cake
      >
      > keeping the rest of variables and values unchanged.
      >
      > Take in account, that I'm only sure that I have the "lunch:" word
      > delimitation (and its values), I don't know if exists "breakfast: " or
      > "dinner:"; but I'm sure that if the variable exists, then the values
      > exists too.
      >
      > Thanks,
      >
      > Sebastian.[/color]


      Comment

      • Rahul Anand

        #4
        Re: RegEx with preg_replace

        A more efficient and compact code to achieve the same with minor
        modifications to ignore multiple space characters in string:

        [SNIP]

        $s = "breakfast: coffee eggs lunch: sandwich apple dinner:
        chicken wine
        cake";

        $food_ids = array( 'coffee' => '00', 'wine' => '01', 'cake' =>
        '02',
        'apple' => '03', 'eggs' => '04', 'sandwich' => '05', 'chicken' =>
        '06' );

        function food_to_code($m atches)
        {
        return preg_replace('/\w+/e','$GLOBALS[\'food_ids\'][\'$0\']',
        trim($matches[0]));
        }

        echo preg_replace_ca llback('/(?<=lunch:\s)(? :[\w\s]+)(?=\s\w+:\s|$ )/',
        'food_to_code', $s);

        [/SNIP]

        A little more compact code (i guess more *efficient* also &
        complicated too):


        [/SNIP]

        $s = "breakfast: coffee eggs lunch: sandwich apple dinner:
        chicken wine
        cake";

        $food_ids = array( 'coffee' => '00', 'wine' => '01', 'cake' =>
        '02',
        'apple' => '03', 'eggs' => '04', 'sandwich' => '05', 'chicken'
        => '06' );

        $pattern = '/(?<=lunch:\s)(? :[\w\s]+)(?=\s\w+:\s|$ )/e';
        $replacement = 'preg_replace(\ '/\\w+/e\',\'$food_ids[\\\'\$0\\\']\'
        ,trim(\'$0\'))' ;

        echo preg_replace($p attern, $replacement,$s );

        [/SNIP]

        --

        Wish you all Happy new year!!

        Cheers!
        Rahul


        "Chung Leong" <chernyshevsky@ hotmail.com> wrote in message news:<ccKdnZl8n eCsNWmiRVn-jA@comcast.com> ...[color=blue]
        > Use preg_replace_ca llback(). In the callback function, parse the list of
        > lunch items and look up the corresponding code for each.
        > In the regexp you need to use preg's lookbehind feature, to look for the
        > string "lunch:" but keeping it out of the match. The following snippet
        > should yield the right result:
        >
        > $s = "breakfast: coffee eggs lunch: sandwich apple dinner: chicken wine
        > cake";
        >
        > $food_ids = array( 'coffee' => '00', 'wine' => '01', 'cake' => '02',
        > 'apple' => '03', 'eggs' => '04', 'sandwich' => '05', 'chicken' => '06' );
        >
        > function food_to_code($m atches) {
        > global $food_ids;
        > $foods = preg_split('/\\s+/', $matches[0]);
        > $codes = "";
        > foreach($foods as $food) {
        > $code = $food_ids[$food];
        > if($code) {
        > $codes .= $code;
        > }
        > }
        > return "$codes ";
        > }
        >
        > echo preg_replace_ca llback('/(?<=lunch:\\s)( ?:\\w+(?:\\s|$) )+/',
        > 'food_to_code', $s);
        >
        > Uzytkownik "Sebastian Araya" <araya.s@numisy s.com.ar> napisal w wiadomosci
        > news:678aed25.0 312310920.d56c5 2f@posting.goog le.com...[color=green]
        > > Hello,
        > >
        > > I have a string like this:
        > >
        > > var1: value1...valueI var2: value1...valueJ ... varN:
        > > value1...valueK
        > >
        > > this is an example:
        > >
        > > breakfast: coffee eggs lunch: sandwich apple dinner: chicken wine
        > > cake
        > >
        > > I'm trying to use preg_replace to change "lunch:" values to a code
        > > number, in order to get in the example:
        > >
        > > breakfast: coffee eggs lunch: 0503 dinner: chicken wine cake
        > >
        > > keeping the rest of variables and values unchanged.
        > >
        > > Take in account, that I'm only sure that I have the "lunch:" word
        > > delimitation (and its values), I don't know if exists "breakfast: " or
        > > "dinner:"; but I'm sure that if the variable exists, then the values
        > > exists too.
        > >
        > > Thanks,
        > >
        > > Sebastian.[/color][/color]

        Comment

        Working...