Solved

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

Posted on 2008-10-23
6
171 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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

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…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
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…

820 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