Reg Exp in PHP

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

    Reg Exp in PHP

    Hello.

    I have a string

    $str = "Thread: 1234567890. Post by Otavio (200.123.123.12 3)";

    and I need to get the *name* before the IP, and after "Post by"....
    The problem is that the number after "Thread" changes, the name too
    and the IP...

    In other words, there are lots of strings and I need to get these
    values.

    Thanks for all.

    Otávio Cordeiro.
    "Graviation cannot be held responsible for people falling in love "
    - Albert Einstein
  • thunder

    #2
    Re: Reg Exp in PHP


    "Otavio" <teivan@bol.com .br> wrote in message
    news:3b4b1a41.0 402102003.4e469 23d@posting.goo gle.com...[color=blue]
    > Hello.
    >
    > I have a string
    >
    > $str = "Thread: 1234567890. Post by Otavio (200.123.123.12 3)";[/color]
    [color=blue]
    > and I need to get the *name* before the IP, and after "Post by"....
    > The problem is that the number after "Thread" changes, the name too
    > and the IP...
    >
    > In other words, there are lots of strings and I need to get these
    > values.
    >
    > Thanks for all.[/color]

    I think this should work:

    $pat = "/^Thread: ([0-9]{1,})\. Post by ([^(]{1,})\(([0-9\.]{1,})\)$/";

    if(preg_match($ pat,$str,$match es)) {
    $threadnum = $matches[1];
    $poster = trim($matches[2]);
    $ipaddr = $matches[3];
    } else {
    echo("no match");
    }

    [color=blue]
    > Otávio Cordeiro.[/color]

    Cheers,
    thunder


    Comment

    • Rahul Anand

      #3
      Re: Reg Exp in PHP

      "thunder" <thunder@mysurn amegoeshere.co. uk> wrote in message news:<102jb4brv 2j4dd@news.supe rnews.com>...[color=blue]
      > "Otavio" <teivan@bol.com .br> wrote in message
      > news:3b4b1a41.0 402102003.4e469 23d@posting.goo gle.com...[color=green]
      > > Hello.
      > >
      > > I have a string
      > >
      > > $str = "Thread: 1234567890. Post by Otavio (200.123.123.12 3)";[/color]
      >[color=green]
      > > and I need to get the *name* before the IP, and after "Post by"....
      > > The problem is that the number after "Thread" changes, the name too
      > > and the IP...
      > >
      > > In other words, there are lots of strings and I need to get these
      > > values.
      > >
      > > Thanks for all.[/color]
      >
      > I think this should work:
      >
      > $pat = "/^Thread: ([0-9]{1,})\. Post by ([^(]{1,})\(([0-9\.]{1,})\)$/";
      >
      > if(preg_match($ pat,$str,$match es)) {
      > $threadnum = $matches[1];
      > $poster = trim($matches[2]);
      > $ipaddr = $matches[3];
      > } else {
      > echo("no match");
      > }
      >
      >[color=green]
      > > Otávio Cordeiro.[/color]
      >
      > Cheers,
      > thunder[/color]

      Another possible RegExp:

      [SNIP]

      $str = "Thread: 1234567890. Post by Otavio (200.123.123.12 3)";
      preg_match("/Thread:\s+(\d+) \.\s+Post
      by\s+([\w\s]+)\s+\(([^)]+)\)/i",$str,$matche s);
      print_r($matche s);

      [/SNIP]

      Will give:
      Thread Number in $matches[1]
      Post By Name in $matches[2]
      IP in $matches[3]


      --
      Cheers,
      Rahul Anand

      Comment

      • Otavio

        #4
        Re: Reg Exp in PHP

        Hello.

        Thank your very much!!

        It's working now. :)
        This string was buggin' me :)

        :)
        Otávio

        rahulanand_bis@ rediffmail.com (Rahul Anand) wrote in message news:<628e2f7b. 0402110529.b2c7 824@posting.goo gle.com>...[color=blue]
        > "thunder" <thunder@mysurn amegoeshere.co. uk> wrote in message news:<102jb4brv 2j4dd@news.supe rnews.com>...[color=green]
        > > "Otavio" <teivan@bol.com .br> wrote in message
        > > news:3b4b1a41.0 402102003.4e469 23d@posting.goo gle.com...[color=darkred]
        > > > Hello.
        > > >
        > > > I have a string
        > > >
        > > > $str = "Thread: 1234567890. Post by Otavio (200.123.123.12 3)";[/color][/color]
        >[color=green][color=darkred]
        > > > and I need to get the *name* before the IP, and after "Post by"....
        > > > The problem is that the number after "Thread" changes, the name too
        > > > and the IP...
        > > >
        > > > In other words, there are lots of strings and I need to get these
        > > > values.
        > > >
        > > > Thanks for all.[/color]
        > >
        > > I think this should work:
        > >
        > > $pat = "/^Thread: ([0-9]{1,})\. Post by ([^(]{1,})\(([0-9\.]{1,})\)$/";
        > >
        > > if(preg_match($ pat,$str,$match es)) {
        > > $threadnum = $matches[1];
        > > $poster = trim($matches[2]);
        > > $ipaddr = $matches[3];
        > > } else {[/color]
        > echo("no match");[color=green]
        > > }
        > >
        > >[color=darkred]
        > > > Otávio Cordeiro.[/color]
        > >
        > > Cheers,
        > > thunder[/color]
        >
        > Another possible RegExp:
        >
        > [SNIP]
        >
        > $str = "Thread: 1234567890. Post by Otavio (200.123.123.12 3)";
        > preg_match("/Thread:\s+(\d+) \.\s+Post
        > by\s+([\w\s]+)\s+\(([^)]+)\)/i",$str,$matche s);
        > print_r($matche s);
        >
        > [/SNIP]
        >
        > Will give:
        > Thread Number in $matches[1]
        > Post By Name in $matches[2]
        > IP in $matches[3][/color]

        Comment

        Working...