| Author |
splitting a string based on a pattern
|
|
| Freelancer71 2008-03-23, 4:03 am |
| Hi,
What I want is to split a string based on the following pattern
starts with '@{'
any number of characters can follow but when it finds a closing curly
bracket it should end. So the following expressions should match
@{x}
@{x_1_23}
@{y|hello|good|}
So lets say
$s = '@{x}+@{y12}=@{ans}fwf{sdfe@{d}wer32r3';
so this should be split in
+
=
fwf{sdfe
wer32r3
I've tried the following code but it doesnt work
<?php
$s = '@{x}+@{y}=@{ans}fwf{sdfe@{d}wer32r3';
$regex = '(@{).+}'; /* doesn't work */
$arr = split($regex,$s);
foreach ( $arr as $a)
echo '<br>' . $a;
?>
Also is there a way to store the patterns that have matched? @{x}, @{y12}
etc
Thanks
| |
| Olaf Schinkel 2008-03-23, 10:12 pm |
|
You are looking for:
preg_match_all
| |
| Freelancer71 2008-03-23, 10:12 pm |
|
"Olaf Schinkel" <blablabla@bluxxxxxxxx.de> wrote in message
news:47e67793$0$23700$9b4e6d93@newsspool
2.arcor-online.net...
>
> You are looking for:
> preg_match_all
>
Thanks! However, what would the regular expression be in this case?
Regards
| |
| PaulB 2008-03-23, 10:12 pm |
| Freelancer71 wrote:
> "Olaf Schinkel" <blablabla@bluxxxxxxxx.de> wrote in message
> news:47e67793$0$23700$9b4e6d93@newsspool
2.arcor-online.net...
>
> Thanks! However, what would the regular expression be in this case?
http://www.greenhithe.org.uk/dev/preg_exp.php
Code
<?php
$table = "@{x}+@{y12}=@{ans}fwf{sdfe@{d}wer32r3";
preg_match_all("|@{(.*)}|U",$table,$rows);
//Loop through the table
foreach ($rows[0] as $row){
//Returned all the contents
echo $row."<br>\n";
}
?>
With thanks to John Dunlop from comp.lang.php without whose help I would not
know this
Paul
| |
| Alexey Kulentsov 2008-03-23, 10:12 pm |
| <?php
$s = '@{x}+@{y12}=@{ans}fwf{sdfe@{d}wer32r3';
print_r(preg_split('/@\\{[^}]+\\}/',$s));
?>
|
|
|
|