views:

70

answers:

1

I have a requirement to insert a string between two markers.

Initially I get a sting (from a file stored on the server) between #DATA# and #END# using:

function getStringBetweenStrings($string,$start,$end){ 
    $startsAt=strpos($string,$start)+strlen($start);
 $endsAt=strpos($string,$end, $startsAt);
 return substr($string,$startsAt,$endsAt-$startsAt);
} 

I do some processing and based on the details of the string, query for some records. If there are records I need to be able to append them at the end of the string and then re-insert the string between #DATA# and #END# within the file on the server.

How can I best achieve this?

Is it possible to insert a record at a time in the file before #END# or is it best to manipulate the string on the server and just re-insert over the existing string in the file on the server?

Example of Data:

AGENT_REF^ADDRESS_1^ADDRESS_2^ADDRESS_3^ADDRESS_4^TOWN^POSTCODE1^POSTCODE2^SUMMARY^DESCRIPTION^BRANCH_ID^STATUS_ID^BEDROOMS^PRICE^PROP_SUB_ID^CREATE_DATE^UPDATE_DATE^DISPLAY_ADDRESS^PUBLISHED_FLAG^LET_RENT_FREQUENCY^TRANS_TYPE_ID^NEW_HOME_FLAG^MEDIA_IMAGE_00^MEDIA_IMAGE_TEXT_00^MEDIA_IMAGE_01^MEDIA_IMAGE_TEXT_01^~

#DATA#

//Property records would appear here and match the string above, each field separated with ^ and terminating with ~

//Once the end of data has been reached, it will be fully terminated with:

#END#

When I check for new properties, I do the following:

  1. Get all existing properties between #DATA# and #END#
  2. Get the IDs of the properties and query for new properties which don't match these IDs

I then need to re-insert the new properties before #END# but after the last property in the file.

The structure of the file is a Rightmove BLM file.

A: 

Just do an str_replace() of the old data with the new:

$str = str_replace('#DATA#'.$oldstr.'#END#', '#DATA#'.$newstr.'#END#', $str);
Will Earp