Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2008-10-23
6
Medium Priority
?
177 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 800 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 1200 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

885 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