Jump to content

[SOLVED] get text between two instances


SetToLoki

Recommended Posts

I'm attempting to connect to a cal dav server and import the info into my own calendar on the web, I have created the protocol to connect to the caldav server and collected the ical file. now I juat need to do the simple task of grabbing all the data between vcalendar start and v calendar end.

What would be the best way to do this? I am guessing preg_match_all.

 

but not sure, any help would be great cheers

Link to comment
Share on other sites

example of file/content?

 

hmm not got one at this pc is a standard ical file

vcalendar: start
somedescription: some value
somedescription: some value
somedescription: some value
vcalendar: end

vcalendar:start
somedescription: some value
somedescription: some value
somedescription: some value
vcalendar: end

 

the file contains an undefined number of these entries the idea is to get each one between start and end and save them to an array.

Link to comment
Share on other sites

I would use file() to read the file into an array with each line a different element of the array. You can then iterate through the array and pick out different pieces of data as needed. You would have the start and end lines as elements, but you could use those when processing the array to differentiate between different records.

Link to comment
Share on other sites

I

I would use file() to read the file into an array with each line a different element of the array. You can then iterate through the array and pick out different pieces of data as needed. You would have the start and end lines as elements, but you could use those when processing the array to differentiate between different records.

I've pulled the file uising webdav and opened it with an xml request so the content is stored as a variable in my class, can file() be used to read the content of a variable?

Link to comment
Share on other sites

...I have created the protocol to connect to the caldav server and collected the ical file. now I juat need to do the simple task of grabbing all the data between vcalendar start and v calendar end.

 

How foolish of me to assume you were talking about a file. Well, then, you could do the same thing as file() by exploding the value on the new line character. I still think processing the file line-by-line appears to be the most efficient. But, not knowing exactly how you are going to use the data, there might be a better method.

Link to comment
Share on other sites

file cannot read the content of a variable.  If you already have the file contents in a variable, you can follow mjdamato's route by using explode with "\n" as the delimiter. Then loop, etc..

 

if you want to go the regex route, you can take your variable that has the content and do this:

 

preg_match_all('~vcalendar: start(.*?)vcalendar: end~is',$content,$matches);

 

$matches[1] will be an array of each set of lines between the start and end.  I don't know what your overall goal here is, but from there, you can do something like this:

 

foreach($matches[1] as $k => $match) {
  $descriptions[$k] = array_filter(explode("\n",$match));
}

 

This will give you a multi-dim array that would look like this:

 

Array
(
    [0] => Array
        (
            [1] => somedescription: some value
            [2] => somedescription: some value
            [3] => somedescription: some value
        )

    [1] => Array
        (
            [1] => somedescription: some value
            [2] => somedescription: some value
            [3] => somedescription: some value
        )

)

Link to comment
Share on other sites

...I have created the protocol to connect to the caldav server and collected the ical file. now I juat need to do the simple task of grabbing all the data between vcalendar start and v calendar end.

 

How foolish of me to assume you were talking about a file. Well, then, you could do the same thing as file() by exploding the value on the new line character. I still think processing the file line-by-line appears to be the most efficient. But, not knowing exactly how you are going to use the data, there might be a better method.

 

is kind of an awkward project buut I am converting the ical data to mysql database, was hoping to grab each set of content into an array between vcal start/end there can be upwards of 900 of these per calendar for over 100 people so time consuming :( then each line in each array has different info for the database I can then generate my web based calendar using the mysql data which is much more efficient than ical (damn apple solutions :D)

 

so a rough diagram would be

 

array[0]

{

  vca: start <-- begin grab data here

  description: value <-- insert this value into databse

  description: value <-- insert this value into databse

  description: value <-- insert this value into databse

  description: value <-- insert this value into databse

  vcal: end <-- end grab data

}

array[1]

{

  vca: start <-- begin grab data here

  description: value <-- insert this value into databse

  description: value <-- insert this value into databse

  description: value <-- insert this value into databse

  description: value <-- insert this value into databse

  vcal: end <-- end grab data

}

and so on as I say there may be 900 or so of these entries they do not always follow the same format ie descrition: value may differ as it only creates that line if the value is used in the calendar it is pulled from ie one may have reminder : 15 minutes where a calendar that doesn't have a reminder set will just not include the line rather than set its value to null.

 

this will be in a script that runs often as it will sync the calendar it is pulling the info from to the calendar on the website.

so it has to run efficiently which is why I picked to come here and seek advice as my way of doing it would probably not be the most efficient

Link to comment
Share on other sites

file cannot read the content of a variable.  If you already have the file contents in a variable, you can follow mjdamato's route by using explode with "\n" as the delimiter. Then loop, etc..

 

if you want to go the regex route, you can take your variable that has the content and do this:

 

preg_match_all('~vcalendar: start(.*?)vcalendar: end~is',$content,$matches);

 

$matches[1] will be an array of each set of lines between the start and end.  I don't know what your overall goal here is, but from there, you can do something like this:

 

foreach($matches[1] as $k => $match) {
  $descriptions[$k] = array_filter(explode("\n",$match));
}

 

This will give you a multi-dim array that would look like this:

 

Array
(
    [0] => Array
        (
            [1] => somedescription: some value
            [2] => somedescription: some value
            [3] => somedescription: some value
        )

    [1] => Array
        (
            [1] => somedescription: some value
            [2] => somedescription: some value
            [3] => somedescription: some value
        )

)

 

seems exactly what I need, thankyou :D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.