Help spliting string by regular expression

This is a discussion on "Help spliting string by regular expression" within the PHP Forum section. This forum, and the thread "Help spliting string by regular expression are both part of the Program Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old Apr 28th, 2007, 12:54
Junior Member
Join Date: Apr 2006
Location: UK
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Help spliting string by regular expression

Hi guys,

I have a string of html which contains comments as well as regular tags. What I want to do is to split the string into an array using comments to determine where the splits should take place.
I have been doing some research and preg_split looks like it will do the job, the trouble is my regular expression writing is worse than useless!

An example of my thinking so far (I know the regular expression is so far wide of the mark it probably isn't worth mentioning!).

PHP: Select all

$str "<!--Comment begins --><p>HTML TAGS HERE</p><!--Comment Ends-->";

$chars preg_split('^<!--Comment begins.<!--Comment Ends-->$' $str);
print_r($chars); 
The opening comment will always take the format <!--Comment Begins (however there maybe extra characters before the comment close) and the closing comment will always be <!--CommentEnds-->.
There will always be a pair of comments with HTML between them however the number of pairs of comments is variable. The HTML between the comments will be different every time.

Any help on writing the reg ex would be very much appreciated!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Apr 30th, 2007, 07:56
Junior Member
Join Date: Apr 2006
Location: UK
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Help spliting string by regular expression

Any ideas on this one guys?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Apr 30th, 2007, 08:03
Reputable Member
Join Date: Jul 2005
Location: Melksham, Wilts, UK
Posts: 293
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Help spliting string by regular expression

Quote:
Originally Posted by Sagaris View Post
Any ideas on this one guys?
Personally, I was't clear from reading your questions whether you were looking to retain the parts between the comments, the parts outside the comment pairs, all sections (but divided at start and end comment), and / or whether or not you want to have the comment strings in your returned array. As no-one else has answered, I wonder if others aren't clear on it either - perhaps if you clarified what you wanted to get out, someone may come back with an answer.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old Apr 30th, 2007, 12:11
Junior Member
Join Date: Apr 2006
Location: UK
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Help spliting string by regular expression

Thanks for your reply Grahame, sorry if I wasn't clear in my original post (looking back on it I can see it wasn't terribly well written!)

I am interested only in the parts of the string between the comments (I would like to keep the comments if possible).


So say the original string was the following

PHP: Select all

$string "<p>HTML BLAH</p>
<!--Comment begins--> First Chunk HTML here<!--Comment Ends-->
<p>More HTML BLAH BLAH</p>
<!--Comment begins--> Second Chunk HTML  here<!--Comment Ends-->
<p>Last bit of HTML BLAH</p>"

I would end up with the following

PHP: Select all

$array[0] = "<!--Comment begins--> First Chunk HTML here<!--Comment Ends-->";
$array[1] = "<!--Comment begins--> Second Chunk HTML here<!--Comment Ends-->"
I hope this makes my objective a bit clearer. Any other questions just ask.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5  
Old Apr 30th, 2007, 13:34
Reputable Member
Join Date: Jul 2006
Location: Scotland
Age: 22
Posts: 357
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Help spliting string by regular expression

try:

<!--Comment begins-->[a-zA-Z0-9]*<!--Comment Ends-->

ignore the caret (^) and dollar sign, they state that the text your looking for is at the beginning and end (respectively) of the body of the text. (Which I'm assuming from your example you don't want).

Also if you had gone along your original lines and used:
<!--Comment begins-->.*<!--Comment Ends-->
The regex would have matched much more of the document than you'd have wished most of the time!

Hope this helps!
Snow
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #6  
Old Apr 30th, 2007, 17:34
Reputable Member
Join Date: Jul 2005
Location: Melksham, Wilts, UK
Posts: 293
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Help spliting string by regular expression

I would go for something like

preg_match_all('/<!--Comment begins-->(.*?)<!--Comment Ends-->/s',$str,$target);

which should put an array of matches into $target.

The use of the *? operator (sparse match) ensures that you get the sections between the first begin and end, then the section between the second begina nd end, and so on. With just a * operator you would only get one match which would be the whole string between the first start and the last finish.

The extra "s" that looks like a typo is to ensure that the new line character matches . in case any of the begin, end pairs have a new line between them.

Although Snow's solution would match the examples in your clarification (if there's a space in the square brackets) it would need extension to work with your earlier example that included tags; .*? is much easier
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #7  
Old Apr 30th, 2007, 17:41
Reputable Member
Join Date: Jul 2006
Location: Scotland
Age: 22
Posts: 357
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Help spliting string by regular expression

I've never come accross '*?' before - very enlightening. Thanks Grahame!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #8  
Old Apr 30th, 2007, 21:23
Junior Member
Join Date: Apr 2006
Location: UK
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Help spliting string by regular expression

Thanks for your reply guys - very much appreciated.

This looks really good - got home late this evening so I haven't been able to test it on my project but I will let you know how it goes tomorrow.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #9  
Old May 1st, 2007, 20:46
Junior Member
Join Date: Apr 2006
Location: UK
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Help spliting string by regular expression

Just to let you know it works a treat. Thanks very much for your help!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Tags
preg_split, regular expression

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
MS Expression radiated Scripts and Online Services 11 Aug 7th, 2007 01:16
Unicode Regular Expression won't work ?? stacioanri JavaScript Forum 2 Jul 28th, 2007 21:52
Extract attribute value with Regular Expression? ScottDC PHP Forum 2 May 18th, 2007 08:37
regular expressions to parse anand1107 JavaScript Forum 1 Mar 21st, 2007 11:32
Need Custom Designer For Regular Work! BlueField Job Opportunities 0 Jun 30th, 2006 23:45


All times are GMT. The time now is 15:30.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42