Solved

Regex to place all contents of <[edit]><[/edit]> in array

Posted on 2008-10-23
6
170 Views
Last Modified: 2012-05-05
I want everything between

<[edit]>
  and
<[/edit]>

To go into an array.  

This should be case insensitive and must be PHP4 compatible.

So in the below example, the following array should be formed:

-> Item 1
-> Item 2
-> Item 3

<[edit]>Item 1<[/edit]> Hello World <[edit]>Item 2<[/edit]> Testing <[edit]>Item 3<[/edit]> The End
0
Comment
Question by:hankknight
  • 3
  • 2
6 Comments
 
LVL 82

Assisted Solution

by:hielo
hielo earned 200 total points
ID: 22786480
try:

preg_match_all('#<\[edit\]>(.*?)<\[/edit\]>#Ui', $str, $matches ) ;
print_r( $matches[1]);
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 22786548
hielo, you will want to leave off the U flag, it causes the ungreedy .*? to be greedy .* which is likely not what you intended:
<pre>
<?php
$str = '<[edit]>Item 1<[/edit]> Hello World <[edit]>Item 2<[/edit]> Testing <[edit]>Item 3<[/edit]>';
preg_match_all('#<\[edit\]>(.*?)<\[/edit\]>#i', $str, $matches ) ;
echo print_r($matches[1],true);
?>

Open in new window

0
 
LVL 27

Accepted Solution

by:
ddrudik earned 300 total points
ID: 22786561
also, we should add the s flag to allow . to match \n:
<pre>
<?php
$str = '<[edit]>Item 1<[/edit]> Hello World <[edit]>Item 2<[/edit]> Testing <[edit]>Item 3<[/edit]>';
preg_match_all('#<\[edit\]>(.*?)<\[/edit\]>#is', $str, $matches ) ;
echo print_r($matches[1],true);
?>

Open in new window

0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 82

Expert Comment

by:hielo
ID: 22788941
Thanks for the info!
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 22789377
Thanks for the question and the points.
0
 
LVL 16

Author Comment

by:hankknight
ID: 22795828
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
preventing spam with mailto 22 29
javascript: add id amounts 5 46
Using VBscript & RegEx, how can I match lines starting with Oracle word 17 57
PHP Syntax Error 4 25
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question