Copying variable part of a string

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

    Copying variable part of a string

    Hello,

    I have strings like these:

    $line = "Item <name> xxx stock, costs yyy and will sell for amount zzz";

    Problem is <name> can consist of 1..n words. Most items are single words,
    but some are up to 8 in my example, and there are is no set maximum (well,
    max is 80 chars I believe, have to look in the stock db, not too relevant I
    think)
    I found this out the hard way. I thought I was clever (never do that again)
    by doing this:
    $words = explode (" ", $line);
    $itemname = $words [1];

    This only works for single-word names.

    Now I want to create or use an existing function that simply copies
    everything between Item and the first numerical substr.
    The function then should look like:

    $itemname = GetSubStr ("Item" , <numerical> , $line);
    <numerical> should be a template of some sort I think.

    Hope I make myself clear, can you help me ?
    TIA
    Pjotr



  • Daniel Tryba

    #2
    Re: Copying variable part of a string

    Pjotr Wedersteers <x33159@westert erp.com> wrote:[color=blue]
    > $line = "Item <name> xxx stock, costs yyy and will sell for amount zzz";[/color]
    ....[color=blue]
    > Now I want to create or use an existing function that simply copies
    > everything between Item and the first numerical substr.
    > The function then should look like:
    >
    > $itemname = GetSubStr ("Item" , <numerical> , $line);
    > <numerical> should be a template of some sort I think.[/color]

    See the manual for regular expressions (eg http://php.net/preg_match):

    /Item(.*?)\d+/


    --

    Daniel Tryba

    Comment

    • steve

      #3
      Re: Re: Copying variable part of a string

      "Daniel Tryba" wrote:[color=blue]
      > Pjotr Wedersteers <x33159@westert erp.com> wrote:[color=green]
      > > $line = "Item <name> xxx stock, costs yyy and will sell[/color]
      > for amount zzz";
      > ....[color=green]
      > > Now I want to create or use an existing function that simply[/color]
      > copies[color=green]
      > > everything between Item and the first numerical substr.
      > > The function then should look like:
      > >
      > > $itemname = GetSubStr ("Item" , <numerical> , $line);
      > > <numerical> should be a template of some sort I think.[/color]
      >
      > See the manual for regular expressions (eg http://php.net/preg_match):
      >
      > /Item(.*?)\d+/
      >
      >[/color]

      preg_match("/Item\s(.*)\s\d+/", $line, $Arr);
      $Item = $Arr[1];

      --
      http://www.dbForumz.com/ This article was posted by author's request
      Articles individually checked for conformance to usenet standards
      Topic URL: http://www.dbForumz.com/PHP-Copying-...ict132890.html
      Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=443944

      Comment

      • Pjotr Wedersteers

        #4
        Re: Re: Copying variable part of a string

        steve wrote:[color=blue]
        > "Daniel Tryba" wrote:[color=green]
        > > Pjotr Wedersteers <x33159@westert erp.com> wrote:[color=darkred]
        > > > $line = "Item <name> xxx stock, costs yyy and will sell[/color]
        > > for amount zzz";
        > > ....[color=darkred]
        > > > Now I want to create or use an existing function that simply[/color]
        > > copies[color=darkred]
        > > > everything between Item and the first numerical substr.
        > > > The function then should look like:
        > > >
        > > > $itemname = GetSubStr ("Item" , <numerical> , $line);
        > > > <numerical> should be a template of some sort I think.[/color]
        > >
        > > See the manual for regular expressions (eg[/color]
        > http://php.net/preg_match): >[color=green]
        > > /Item(.*?)\d+/
        > >
        > >[/color]
        >
        > preg_match("/Item\s(.*)\s\d+/", $line, $Arr);
        > $Item = $Arr[1];[/color]

        Thanks for the tips! I'm studying regexp now... Geez, I've seen less complex
        ones in my life, lol. gettin' old!
        KR
        Pjotr


        Comment

        • steve

          #5
          Re: Re: Re: Copying variable part of a string

          "Pjotr Wedersteers" wrote:[color=blue]
          > steve wrote:[color=green]
          > > "Daniel Tryba" wrote:[color=darkred]
          > > > Pjotr Wedersteers <x33159@westert erp.com> wrote:
          > > > > $line = "Item <name> xxx stock, costs yyy and[/color][/color]
          > will sell[color=green][color=darkred]
          > > > for amount zzz";
          > > > ....
          > > > > Now I want to create or use an existing function that[/color][/color]
          > simply[color=green][color=darkred]
          > > > copies
          > > > > everything between Item and the first numerical[/color][/color]
          > substr.[color=green][color=darkred]
          > > > > The function then should look like:
          > > > >
          > > > > $itemname = GetSubStr ("Item" , <numerical> ,[/color][/color]
          > $line);[color=green][color=darkred]
          > > > > <numerical> should be a template of some sort I[/color][/color]
          > think.[color=green][color=darkred]
          > > >
          > > > See the manual for regular expressions (eg[/color]
          > > http://php.net/preg_match): >[color=darkred]
          > > > /Item(.*?)\d+/
          > > >
          > > >[/color]
          > >
          > > preg_match("/Item\s(.*)\s\d+/", $line, $Arr);
          > > $Item = $Arr[1];[/color]
          >
          > Thanks for the tips! I’m studying regexp now... Geez, I’ve
          > seen less complex
          > ones in my life, lol. gettin’ old!
          > KR
          > Pjotr[/color]

          Pjotr, regex is hard to learn, but once learned, you cannot do without
          it.

          --
          http://www.dbForumz.com/ This article was posted by author's request
          Articles individually checked for conformance to usenet standards
          Topic URL: http://www.dbForumz.com/PHP-Copying-...ict132890.html
          Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=444336

          Comment

          Working...