PHP:替换双引号

时间:2016-06-17 22:12:09

标签: php string str-replace

我有从CMS生成的文本,因此我无法控制输出的字符串。

我已尝试过各种PHP函数来替换双引号,但没有用。有人可以提出解决方案吗?

<?php
$comments = str_replace('"', "'", ("6:00 pm , practiced "Zen' flying  and sit carving (one leg down) and back carving and sit-to-sit front flip (weight require slower wind speed)");

echo $comments;
?>

错误讯息:

Parse error: syntax error, unexpected 'Zen' (T_STRING) in

从模板中添加完整代码:

    {exp:query
  sql="SELECT 
  t.entry_id, 
  entry_date, 
  field_id_4 as tunnel, 
  field_id_2 as log_time, 
  field_id_17 as log_video, 
  field_id_18 as log_comments
  FROM exp_channel_data d, exp_channel_titles t
  WHERE t.channel_id = 7
  AND t.status != 'Delete'
  AND author_id = '{embed:member_id}'
  AND t.entry_id = d.entry_id
  AND field_id_19 = ''
  ORDER BY entry_id DESC"
}
  <?php 
  //strip out Playa bumph from our tunnel
    $tunnel = substr(strrchr("{tunnel}","]"),2);
    preg_match_all("/\[[^\]]*\]/", "{tunnel}", $matches);
    $tunnel_id = trim($matches[0][0],'[,]');

  $time[] = array(
        "entry_id" => "{entry_id}", 
        "tunnel" => $tunnel,
    "tunnel_id" => $tunnel_id,
        "entry_date" => "{entry_date}", 
        "log_time" => "{log_time}", 
        "log_video" => "{log_video}", 
        "log_comments" => "{log_comments}"); 
  ?>
{/exp:query}

<script>
   IBA.logged_time =  <?php  echo json_encode($time); ?>
</script>

log_comments就是问题所在。

3 个答案:

答案 0 :(得分:1)

您需要在字符串文字中转义双引号。还需要删除一个不匹配的左括号:

<?php
$comments = str_replace('"', "'", "6:00 pm , practiced \"Zen' flying  and sit carving (one leg down) and back carving and sit-to-sit front flip (weight require slower wind speed)");

echo $comments;
?>

输出:

6:00 pm , practiced 'Zen' flying  and sit carving (one leg down) and back carving and sit-to-sit front flip (weight require slower wind speed)

编辑:由于您发布了更多代码,我会看到发生了什么。请尝试以下代码。它将起作用,除非内容在某处有“LOG_COMMENTS \ n”。

$time[] = array(
    "entry_id" => "{entry_id}", 
    "tunnel" => $tunnel,
    "tunnel_id" => $tunnel_id,
    "entry_date" => "{entry_date}", 
    "log_time" => "{log_time}", 
    "log_video" => "{log_video}", 
    "log_comments" => <<<LOG_COMMENTS
{log_comments}
LOG_COMMENTS
);

然而,这是一个非常糟糕的设计。 CMS是否有理由无法将数据保存到数据库或纯文本文件中?

答案 1 :(得分:0)

{log_comments}修改为{addslashes(log_comments)}

你需要逃避你正在构建的字符串的输入,或者字符串本身会被流氓引号分解。

docs

请参阅{{3}}

答案 2 :(得分:0)

您可能应该将这些文本放在某个变量中。 首先确保你没有出现语法错误。可能是因为您刚刚从浏览器中复制了这些文本并希望将其放在str_replace()上。 你必须像这样逃避引用。

$comments = '("6:00 pm , practiced "Zen\' flying  and sit carving (one leg down) and back carving and sit-to-sit front flip (weight require slower wind speed)"';

使用斜杠。 然后,只有您可以在代码中使用该部分

$comments = str_replace('"', "'", '("6:00 pm , practiced "Zen\' flying  and sit carving (one leg down) and back carving and sit-to-sit front flip (weight require slower wind speed)"');

OR 你可以这样做

$comments = '("6:00 pm , practiced "Zen\' flying  and sit carving (one leg down) and back carving and sit-to-sit front flip (weight require slower wind speed)"';

$com = htmlspecialchars ($comments);

$replacedText = str_replace("&quot;","&#039;",$com);

echo $com;

echo "<br/>";

echo $replacedText;

首先将特殊字符转换为HTML实体。 htmlspecialchars 然后,您可以使用str_replace函数轻松替换引号。

希望你得到错误的地方。

相关问题