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.
|
|
|
|
|
![]() |
||
Help spliting string by regular expression
|
||
| Notices |
![]() |
|
|
LinkBack | Thread Tools |
|
#1
|
|||
|
|||
|
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!).
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! |
|
|
|
#2
|
|||
|
|||
|
Re: Help spliting string by regular expression
Any ideas on this one guys?
|
|
#3
|
|||
|
|||
|
Re: Help spliting string by regular expression
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.
|
|
#4
|
|||
|
|||
|
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
|
|
#5
|
|||
|
|||
|
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 |
|
#6
|
|||
|
|||
|
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 |
|
#7
|
|||
|
|||
|
Re: Help spliting string by regular expression
I've never come accross '*?' before - very enlightening. Thanks Grahame!
|
|
#8
|
|||
|
|||
|
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. |
|
#9
|
|||
|
|||
|
Re: Help spliting string by regular expression
Just to let you know it works a treat. Thanks very much for your help!
|
![]() |
| Tags |
| preg_split, regular expression |
| Thread Tools | |
|
|
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 |