dapcigar Posted November 18, 2014 Share Posted November 18, 2014 Am trying to save a text input that require the use of some php special characters like (" ", ' ' ) e.t.c. When ever i post the data, the system returns an error. please, how can i go about this? Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/292535-special-characters-problem/ Share on other sites More sharing options...
jazzman1 Posted November 18, 2014 Share Posted November 18, 2014 Here-doc. $msg = <<<EOD Example of string containing spacial (',", chars using heredoc syntax. EOD; echo $msg; Quote Link to comment https://forums.phpfreaks.com/topic/292535-special-characters-problem/#findComment-1496870 Share on other sites More sharing options...
Jacques1 Posted November 18, 2014 Share Posted November 18, 2014 Be aware that heredocs are interpreted like double-quoted strings, so character sequences like $x or \n will be interpreted in a special way. If you want plain text like in a single-quoted string, use a nowdoc: <?php $str = <<<'MYTEXT' Example of string spanning multiple lines using nowdoc syntax. MYTEXT; Quote Link to comment https://forums.phpfreaks.com/topic/292535-special-characters-problem/#findComment-1496887 Share on other sites More sharing options...
dapcigar Posted November 18, 2014 Author Share Posted November 18, 2014 In a case where i want to accept the input from a user, can i do it this way? <?php $comment = $_POST['comment']; $str = <<<'$comment' Example of string spanning multiple lines using nowdoc syntax. $comment; Quote Link to comment https://forums.phpfreaks.com/topic/292535-special-characters-problem/#findComment-1496898 Share on other sites More sharing options...
Jacques1 Posted November 18, 2014 Share Posted November 18, 2014 This makes absolutely no sense. The part after the “<<<” is the delimiter for the heredoc/nowdoc block. It simply tells PHP where the block begins and where it ends. You do not use variables or anything like that. Just make up an identifier: <?php $comment = $_POST['comment']; $str = <<<'I_AM_THE_DELIMITER' Example of string spanning multiple lines using nowdoc syntax. I_AM_THE_DELIMITER; If you want to insert some variable, you have to insert it into the block between the delimiters: <?php $comment = $_POST['comment']; $str = <<<I_AM_THE_DELIMITER This is the user comment: $comment I_AM_THE_DELIMITER; Note that you need a heredoc if you want to directly insert variables like in a double-quoted string. A nowdoc doesn't do that (as I already explained). Quote Link to comment https://forums.phpfreaks.com/topic/292535-special-characters-problem/#findComment-1496900 Share on other sites More sharing options...
mac_gyver Posted November 18, 2014 Share Posted November 18, 2014 back to your original problem, where are you trying to save this text input to? a database? a text file? and what errors are you getting? Quote Link to comment https://forums.phpfreaks.com/topic/292535-special-characters-problem/#findComment-1496908 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.