douchuanghan1344 2010-04-08 07:59
浏览 102

在两个标记之间插入字符串

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.

  • 写回答

2条回答 默认 最新

  • du1462 2010-04-08 11:15
    关注

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

    $str = str_replace('#DATA#'.$oldstr.'#END#', '#DATA#'.$newstr.'#END#', $str);
    
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向