need help for regex

This is a discussion on need help for regex within the PHP Language forums, part of the PHP Programming Forums category; Hi, I need a regex (may be together with a php function) to retrieve information within '[' and ']' in a line ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 06-02-2004
Alex Shi
 
Posts: n/a
Default 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!
http://www.pocellular.com
==================================================
Reply With Quote
  #2 (permalink)  
Old 06-02-2004
Chris Hope
 
Posts: n/a
Default Re: need help for regex

Alex Shi wrote:

> 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.


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

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/
Reply With Quote
  #3 (permalink)  
Old 06-02-2004
Matthias Esken
 
Posts: n/a
Default Re: need help for regex

Alex Shi wrote:

> 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.


// 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($matches);

// 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
Reply With Quote
  #4 (permalink)  
Old 06-03-2004
Alex Shi
 
Posts: n/a
Default Re: need help for regex

> Alex Shi wrote:
>
> > 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.

>
> $foo = 'hello world, example [demo|123] line with some [$55] yesys [no].';
> preg_match_all("/\[(.*)\]/U", $foo, $matches);
> print_r($matches);
>
> 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/


Thanks!

Alex Shi

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 08:29 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0