string - PHP: replace double quotes

327

I have text that is being generated from a CMS so I can't control the string that is output.

I've tried all kinds of PHP functions to replace double quotes with single but to no avail. Can anyone suggest a solution?

<?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;
?>

Error Message:

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

Adding FULL code from template:

    {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 is in the problem.

909

Answer

Solution:

You need to escape double quotes in your string literal. There's also an umatched left-parenthesis you need to remove:

<?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;
?>

Output:

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)

Edit: Since you've posted more code I see what is going on. Try the following code. It will work unless the content has "LOG_COMMENTS\n" in it somewhere.

$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
);

Still, this is a very poor design. Is there a reason the CMS can't save data into a database or a plain text file?

2

Answer

Solution:

Edit{log_comments} to{addslashes(log_comments)}

You need to escape the input of your string you are in the process of building or that string will itself be broken up by the rogue quotes.

What it means to escape a string.

See the docs

84

Answer

Solution:

You probably should get those text inside some variable. First make sure you have not made a syntax mistake. May be because you just copied those text from browser and want to put it on str_replace(). You must escape the quote like this.

$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)"';

By using slash. Then only you can use that part in your code

$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 you can do like this

$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;

so first Convert special characters to HTML entities. htmlspecialchars Then you can easily replace the quote using the str_replace function.

Hope you get where you made mistake.

People are also looking for solutions to the problem: php - Images for new added or created products in magento have larger size

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.