-2

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.

KoalaKid
  • 232
  • 2
  • 11

3 Answers3

1

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?

Shira
  • 6,392
  • 2
  • 25
  • 27
0

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

Community
  • 1
  • 1
Jonathan
  • 10,936
  • 8
  • 64
  • 79
  • I've tried that. I know this should be incredibly simple, I must have done this 100 times but it just keeps kicking out an error. – KoalaKid Jun 17 '16 at 22:30
  • Then trace the error back. Read out the error to us. Break up your code to multiple lines if you have to if you don't get enough feedback from the error to say what line number and column it is. I feel that only you are going to be able to solve your own problem at this point, but the crux of the issue here is apparently the need to escape your incoming string stream. – Jonathan Jun 17 '16 at 22:33
  • Agreed. I've broken it out into just a plain PHP file with one of the problem strings and tried every kind of PHP function I can think of. – KoalaKid Jun 17 '16 at 22:37
  • You see if you can get this copy to work: "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" – KoalaKid Jun 17 '16 at 22:38
  • Of course you can't paste in a broken string. You can't use a broken string anywhere. The point is, the machine will fall over if you give it bad data. Your CMS would have fallen over already if it was supplying you with broken strings - it would not have been able to send them to you in the first place. Bad data in = bad data out. If you are *adamant* it is not you, then it must be some intermediate processing you are doing to the data coming from the CMS. You still haven't clearly described what exactly the CMS is - and what the endpoint is from which you are deriving the data. – Jonathan Jun 17 '16 at 22:43
  • I hear you but that is the data I have to work with. Unless I go into the database and update every entry to remove quotes that's what I have to play with. I can see it when I run the SQL query in the DB. – KoalaKid Jun 17 '16 at 22:45
  • What you see pretty-printed is not necessarily what you *get*. You need to describe - concretely - what [box] ---> is outputting string "xyz". Looking at your code, it is impossible to tell what is going on. We have zero perspective here. – Jonathan Jun 17 '16 at 22:47
0

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.