Jamie Jamie - 12 days ago 8
PHP Question

Can't get quotes right

and a you saw on the title. I can't get the quotes right with php. I want to export some variables to another file, but i can't get it quoted right, anybody that can help me?

This is my code:

if($sliderconfig = fopen("./slidersettings.class.php", "w")) {
$error[] = 'Er was een probleem met het veranderen van de slider_config, contacteer site-admin.';
}
$configuration = '<?php
$slider_text = '.$slider_text.';
$text_link = '.$text_link.';
$slider_speed = '.$slider_speed.';
$text_size = '.$text_size.';
?>';
fwrite($sliderconfig, $configuration);
fclose($sliderconfig);
$message[] = 'De configuratie is succesvol bijgwerkt!';


This is the ouput:

<?php
$slider_text = test;
$text_link = test;
$slider_speed = test;
$text_size = test;
?>


And this should be the ouput:

<?php
$slider_text = 'test';
$text_link = 'test';
$slider_speed = 'test';
$text_size = 'test';
?>


Anyone that nows how to fix this, if so and you answer it down here. Thanks in advance.

Ben Ben
Answer

You should escape the single quotes in the string by using a backslash \ before the single quote like so:

$configuration = '<?php
    $slider_text = \''.$slider_text.'\';
    $text_link = \''.$text_link.'\';
    $slider_speed = \''.$slider_speed.'\';
    $text_size = \''.$text_size.'\';
    ?>';

Alternatively you can use double quotes in the string and single quotes to encase the string:

$configuration = '<?php
    $slider_text = "'.$slider_text.'";
    $text_link = "'.$text_link.'";
    $slider_speed = "'.$slider_speed.'";
    $text_size = "'.$text_size.'";
    ?>';