need help for regex

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

    need help for regex

    Hi,

    I need a regex (may be together with a php function) to retrieve
    information within '[' and ']' in a line of text. Here is an example:

    hello world, example [demo|123] line with some [$55] yesys [no].

    I need to retrieve all the three pieces of text enclosed in [] pairs.

    Thanks in advance!

    Alex Shi


    --
    =============== =============== =============== =====
    Cell Phone Batteries at 30-50%+ off retail prices!

    =============== =============== =============== =====
  • Matthias Esken

    #2
    Re: need help for regex

    Alex Shi wrote:
    [color=blue]
    > I need a regex (may be together with a php function) to retrieve
    > information within '[' and ']' in a line of text. Here is an example:
    >
    > hello world, example [demo|123] line with some [$55] yesys [no].
    >
    > I need to retrieve all the three pieces of text enclosed in [] pairs.[/color]

    // retrieve data
    $string =
    "hello world, example [demo|123] line with some [$55] yesys [no].";
    preg_match_all( '~\[([^\]]*)\]~', $string, $matches);

    // look at the result
    print_r($matche s);

    // print all values
    foreach ($matches[1] as $value) {
    echo($value . '<br>');
    }

    // access first value
    echo($matches[1][0] . '<br>');

    // access third value
    echo($matches[1][2] . '<br>');


    Regards,
    Matthias

    Comment

    • Chris Hope

      #3
      Re: need help for regex

      Alex Shi wrote:
      [color=blue]
      > I need a regex (may be together with a php function) to retrieve
      > information within '[' and ']' in a line of text. Here is an example:
      >
      > hello world, example [demo|123] line with some [$55] yesys [no].
      >
      > I need to retrieve all the three pieces of text enclosed in [] pairs.[/color]

      $foo = 'hello world, example [demo|123] line with some [$55] yesys [no].';
      preg_match_all( "/\[(.*)\]/U", $foo, $matches);
      print_r($matche s);

      This gives you:
      Array
      (
      [0] => Array
      (
      [0] => [demo|123]
      [1] => [$55]
      [2] => [no]
      )

      [1] => Array
      (
      [0] => demo|123
      [1] => $55
      [2] => no
      )

      )


      --
      Chris Hope
      The Electric Toolbox - http://www.electrictoolbox.com/

      Comment

      • Alex Shi

        #4
        Re: need help for regex

        > Alex Shi wrote:[color=blue]
        >[color=green]
        > > I need a regex (may be together with a php function) to retrieve
        > > information within '[' and ']' in a line of text. Here is an example:
        > >
        > > hello world, example [demo|123] line with some [$55] yesys [no].
        > >
        > > I need to retrieve all the three pieces of text enclosed in [] pairs.[/color]
        >
        > $foo = 'hello world, example [demo|123] line with some [$55] yesys [no].';
        > preg_match_all( "/\[(.*)\]/U", $foo, $matches);
        > print_r($matche s);
        >
        > This gives you:
        > Array
        > (
        > [0] => Array
        > (
        > [0] => [demo|123]
        > [1] => [$55]
        > [2] => [no]
        > )
        >
        > [1] => Array
        > (
        > [0] => demo|123
        > [1] => $55
        > [2] => no
        > )
        >
        > )
        >
        >
        > --
        > Chris Hope
        > The Electric Toolbox - http://www.electrictoolbox.com/[/color]

        Thanks!

        Alex Shi

        Comment

        Working...