【问题标题】:Concatenation PHP preg_replace function连接 PHP preg_replace 函数
【发布时间】:2015-07-05 13:48:11
【问题描述】:

我正在使用 Ajax 将 JS 变量传输到我的脚本 PHP 以更改背景颜色。你能帮我实现这个目标吗?我不知道如何在这里使用连接并使用 $mavariable。

$mavariable=$_POST['background'];
echo $mavariable; //Return the string blue

$backgroundcolor='background-color:green;';
$background='/background-color:yellow;/'; //How to use $mavariable here ? 

$contenuMod=preg_replace($background, $backgroundcolor,$lineContent); //This works if i'm not using $mavariable 

【问题讨论】:

  • 请详细说明。你如何使用$mavariable
  • 替换字符串“yellow”

标签: javascript php ajax string concatenation


【解决方案1】:

只需 concatenate 将变量与 string 放在你需要的地方。试试 -

$background='/background-color:" . $mavariable . ";/';

【讨论】:

  • 你需要使用"OR',不要混用:)
【解决方案2】:

使用.(dot) 运算符可以连接两个字符串或值,如下所示

$background='background-color:'.$mavariable;

这将帮助您解决问题

【讨论】:

    【解决方案3】:

    要完成 sgt 的回答,您应该更改正则表达式以处理更多颜色(文字样式):

    background-color:\w+;
    

    或者您可以使用十六进制样式的正则表达式:

    background-color:#[A-Fa-f0-9]+;
    

    【讨论】:

      【解决方案4】:

      我们接近真相...这是生成代码:

          blue  // Result of echo $mavariable
          Chaine nouvelle :body{     
          Chaine nouvelle :    background-color:".$mavariable.";
          Chaine nouvelle : font-size: 18px;
          Chaine nouvelle :    font-family: cursive;
          Chaine nouvelle :}}}
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-06-19
        • 1970-01-01
        • 2014-04-28
        • 2012-10-10
        • 2015-07-05
        • 1970-01-01
        • 2013-12-27
        • 1970-01-01
        相关资源
        最近更新 更多