Solved

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

Posted on 2008-10-23
6
168 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

932 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now