Insert string between two markers
        Posted  
        
            by user275074
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by user275074
        
        
        
        Published on 2010-04-08T07:59:37Z
        Indexed on 
            2010/04/08
            8:03 UTC
        
        
        Read the original article
        Hit count: 451
        
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?
© Stack Overflow or respective owner