douhan9619 2016-02-09 19:45
浏览 30
已采纳

使用字符串中的信息替换PHP中的字符串

My current customer is aiming for a closed text-viewer, while keeping certain variables open for customers. This is to show a preview of the text, while not relying on the client side for the whole text. At the moment, I think of a form, which contains input elements. These elements will dynamically be filled into certain spans in the text so the client is able to see the preview. When the form is submitted, the server side will replace the identifiers with the inputs.

My current idea is to let my client create a text which contains identifiers, which the client can fill in or show with checkboxes. All these elements are stored in a mysql database, which can result in something like this:

You are thinking about %el1%, and you want to create %el2%. %bl3%

This is using the following inputs.

<input type="text" name="el1">
<input type="text" name="el2">
<input type="checkbox" name="bl3">

To replace these identifiers with A) elements which can be changed by Javascript and B) can be replaced by PHP in the full document.

You are thinking about <span id="el1"></span>, and you want to create <span id="el2"></span>. <span id="bl3" style="display:none;">This is a text that is retrieved from the database, and shown when the user checks the checkbox</span>

I don't know if this is a good idea at all, but the client wishes it to be this way. In this way, the client can't edit the full text, but only input certain inputs. I will validate these both client- and serverside.

Is this a good idea? If no, what should I do instead? And if yes, how can I create a loop that will find these identifiers based on %el1% or %bl3% and replace these with <span> tags or the submitted variables?

  • 写回答

2条回答 默认 最新

  • dqx76962 2016-02-09 19:55
    关注
    $phrase  = "You are thinking about %el1%, and you want to create %el2%. %bl3%.";
    $healthy = array("%el1%", "%el2%", "%bl3%");
    $yummy   = array("el1", "el2", "bl3");
    $newphrase = str_replace($healthy, $yummy, $phrase);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?