doujiaozhao2489 2011-11-30 16:34
浏览 67
已采纳

PHP:当变量(b)包含文本时,如何在另一个变量(b)中显示变量(a)

I am storing text in my database. Here is the following text:

".$teamName.", is the name of a recently formed company hoping to take over the lucrative hairdryer design ".$sector."

After querying the database I assign this text to a variable called $news, then echo it.

However the text is outputted to the screen exactly as above without the variables $teamName*and $sector replaced by there corresponding values.

I assure you that both $teamName and $sector are defined before I query the database.

Is it even possible to do what I am trying to do?

  • 写回答

6条回答 默认 最新

  • drvntaomy06331839 2011-11-30 16:38
    关注

    You might be better off using sprintf() here.

    $string = "%s is the name of a recently formed company hoping to take over the lucrative hairdryer design %s.";
    
    $teamName = "My Company";
    $sector = "sector";
    
    echo sprintf($string, $teamName, $sector);
    // My Company is the name of a recently formed company hoping to take over the lucrative hairdryer design sector.
    

    In your database, you store $string. Use sprintf() to substitute the variable values.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?