regular expression problem (preg_replace)

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

    regular expression problem (preg_replace)

    hi all

    using preg_replace

    how can i replace the letter i in a string with nothing (delete it)
    when it is the last letter or it is followed by an i?

    i have products that are listed in a db with i or ii as in 320ii and i
    want to strip out the i's at the end when displaying the product name

    thanks

    marc

  • Rik

    #2
    Re: regular expression problem (preg_replace)

    monomaniac21 wrote:
    hi all
    >
    using preg_replace
    >
    how can i replace the letter i in a string with nothing (delete it)
    when it is the last letter or it is followed by an i?
    >
    i have products that are listed in a db with i or ii as in 320ii and i
    want to strip out the i's at the end when displaying the product name
    With last latter, do you mean last letter in a word or the real last
    character.
    Word:
    preg_replace('/i(\b|i)/i','$1',$string );
    Dead last:
    preg_replace('/i($|i)/si','$1',$strin g);

    If you want to strip out ALL ending i's:
    preg_replace('/i+(\b)/i','$1',$string );
    --
    Rik Wasmus


    Comment

    • Rik

      #3
      Re: regular expression problem (preg_replace)

      Rik wrote:
      preg_replace('/i($|i)/si','$1',$strin g);
      No need for the /s modifier offcourse...
      --
      Rik Wasmus


      Comment

      • monomaniac21

        #4
        Re: regular expression problem (preg_replace)


        Rik wrote:
        Rik wrote:
        preg_replace('/i($|i)/si','$1',$strin g);
        >
        No need for the /s modifier offcourse...
        --
        Rik Wasmus
        hi rik that works great for instances of a single i at the end of the
        string (last character) but i also need it to strip out any i's that
        occur next to other i's so for example:

        230ii := 230, 230iiiiii := 230,
        just as 230i := 230

        thanks for your help

        marc

        Comment

        • Rik

          #5
          Re: regular expression problem (preg_replace)

          monomaniac21 wrote:
          Rik wrote:
          >Rik wrote:
          >>preg_replace( '/i($|i)/si','$1',$strin g);
          >>
          >No need for the /s modifier offcourse...
          >--
          >Rik Wasmus
          >
          hi rik that works great for instances of a single i at the end of the
          string (last character) but i also need it to strip out any i's that
          occur next to other i's so for example:
          >
          230ii := 230, 230iiiiii := 230,
          just as 230i := 230
          >
          The last one should work:
          preg_replace('/i+(\b)/i','$1',$string );
          --
          Rik Wasmus


          Comment

          • william

            #6
            Re: regular expression problem (preg_replace)

            On Wed, 03 Jan 2007 07:37:02 -0800, monomaniac21 wrote:
            hi all
            >
            using preg_replace
            >
            how can i replace the letter i in a string with nothing (delete it)
            when it is the last letter or it is followed by an i?
            >
            i have products that are listed in a db with i or ii as in 320ii and i
            want to strip out the i's at the end when displaying the product name
            >
            thanks
            >
            marc
            if you have a huge database it might be better to use stored procedure :

            (vars between "{}" have to be adapted to your table/field)



            DELIMITER ?
            CREATE PROCEDURE sp ()
            BEGIN
            DECLARE id INT;
            DECLARE currentid INT DEFAULT 0;
            DECLARE productname VARCHAR(128);
            DECLARE newproductname VARCHAR(128);
            DECLARE maxid INT DEFAULT 0;
            SELECT max({product_id }) INTO maxid FROM {product} where {product_id} >
            current AND {product_name} REGEXP 'ii$';

            WHILE ( current < maxid+1 ) DO

            SELECT {product_id},{p roduct_name} INTO id,productname FROM {product}
            where {product_id} current AND {product_name} REGEXP 'ii$' LIMIT 1;
            newproductname = LEFT(productnam e,LENGTH(produc tname)-1);

            UPDATE {product} set {product_name} = newproductname WHERE {product_id}
            =id;

            current=id;

            END WHILE;
            END?
            DELIMITER ;

            Comment

            • Petr Vileta

              #7
              Re: regular expression problem (preg_replace)

              "monomaniac 21" <mcyi2mr3@googl email.compíse v diskusním príspevku
              news:1167838622 .294198.287760@ n51g2000cwc.goo glegroups.com.. .
              hi all
              >
              using preg_replace
              >
              how can i replace the letter i in a string with nothing (delete it)
              when it is the last letter or it is followed by an i?
              >
              i have products that are listed in a db with i or ii as in 320ii and i
              want to strip out the i's at the end when displaying the product name
              >
              preg_replace('/^([^i]+)i*$/i','$1',$string );

              320i will be 320
              320ii will be 320


              preg_replace('/^(.+)i*$/i','$1',$string );

              320i will be 320
              320ii will be 320i

              --

              Petr Vileta, Czech republic
              (My server rejects all messages from Yahoo and Hotmail. Send me your mail
              from another non-spammer site please.)




              Comment

              • Curtis

                #8
                Re: regular expression problem (preg_replace)

                <?php
                $regex = '
                /
                ([^i]+) # product name before i. You could also negate \s to
                validate, if necessary
                i+(?:\b|$) # one or more is before a word boundary or end of line
                /x';

                $product = preg_replace($r egex, '$1', $product);
                ?>

                Just tested it, and it seems to work how the OP specified. This will
                replace all i's following the product name:

                $p = '320i 320ii 400 555iiii';

                Becomes

                320 320 400 555

                Curtis

                On Jan 3, 7:37 am, "monomaniac 21" <mcyi2...@googl email.comwrote:
                hi all
                >
                using preg_replace
                >
                how can i replace the letter i in a string with nothing (delete it)
                when it is the last letter or it is followed by an i?
                >
                i have products that are listed in a db with i or ii as in 320ii and i
                want to strip out the i's at the end when displaying the product name
                >
                thanks
                >
                marc

                Comment

                Working...