Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

splitting a string based on a pattern
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



Report this thread to moderator Post Follow-up to this message
Old Post
Freelancer71
03-23-08 09:03 AM


Re: splitting a string based on a pattern
You are looking for:
preg_match_all


Report this thread to moderator Post Follow-up to this message
Old Post
Olaf Schinkel
03-24-08 03:12 AM


Re: splitting a string based on a pattern
"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



Report this thread to moderator Post Follow-up to this message
Old Post
Freelancer71
03-24-08 03:12 AM


Re: splitting a string based on a pattern
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



Report this thread to moderator Post Follow-up to this message
Old Post
PaulB
03-24-08 03:12 AM


Re: splitting a string based on a pattern
<?php

$s =  '@{x}+@{y12}=@{ans}fwf{sdfe@{d}wer32r3';


print_r(preg_split('/@\\{[^}]+\\}/',$s));

?>

Report this thread to moderator Post Follow-up to this message
Old Post
Alexey Kulentsov
03-24-08 03:12 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PHP Language archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 01:43 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.