Solved

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

Posted on 2008-10-23
6
172 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…

751 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