AV1611 Posted October 7, 2007 Share Posted October 7, 2007 How do you do this without manually escaping the " in the string (oh, and the \ as well)? $str="1234567890 ';:|'"/\<>?][}{)(*&^%$#@!~ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; addslashes doesn't seem to work that way. Link to comment https://forums.phpfreaks.com/topic/72200-create-str-with-special-characters/ Share on other sites More sharing options...
Rithiur Posted October 7, 2007 Share Posted October 7, 2007 Well.. Uhh, you could use HEREDOC syntax, maybe... like $str = <<<CHARS 1234567890 ';:|'"/\<>?][}{)(*&^%$#@!~ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ CHARS; In this particular case, the $ does not create a problems, since it's not followed by valid variable name, but if it's followed by anything that makes it valid variable, it will parse as variable. Link to comment https://forums.phpfreaks.com/topic/72200-create-str-with-special-characters/#findComment-364172 Share on other sites More sharing options...
tibberous Posted October 7, 2007 Share Posted October 7, 2007 Trick question! Use ' instead of ", and escape the '! & [sOLVED] Link to comment https://forums.phpfreaks.com/topic/72200-create-str-with-special-characters/#findComment-364179 Share on other sites More sharing options...
AV1611 Posted October 8, 2007 Author Share Posted October 8, 2007 Trick question! Use ' instead of ", and escape the '! & [sOLVED] Um, that solves nothing. You can create a variable with CHARS? How do I deal with the $ as you stated above? I have a string that could have anything the type decided to type into the field... Link to comment https://forums.phpfreaks.com/topic/72200-create-str-with-special-characters/#findComment-364980 Share on other sites More sharing options...
Rithiur Posted October 8, 2007 Share Posted October 8, 2007 Well, if you are getting the variable from user input and would like to save it into an executable PHP, then you could simply replace \ with \\ and ' with \' and save it inside single quotes. For example, the following will var_dump the original value that was given to str_replace <?php $string = str_replace( array("\\", "'"), array("\\\\", "\\'"), "1234567890 ';:|'\"/\<>?][}{)(*&^%$#@!~ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"); $string = eval("return '$string';"); var_dump($string); ?> Link to comment https://forums.phpfreaks.com/topic/72200-create-str-with-special-characters/#findComment-364992 Share on other sites More sharing options...
AV1611 Posted October 9, 2007 Author Share Posted October 9, 2007 You still haven't solved my problem. How do I get the string to add the slashes automatically to begin with? addslashes doesn't do it... Link to comment https://forums.phpfreaks.com/topic/72200-create-str-with-special-characters/#findComment-365484 Share on other sites More sharing options...
kenrbnsn Posted October 9, 2007 Share Posted October 9, 2007 What do you mean by How do I get the string to add the slashes automatically to begin with? addslashes doesn't do it... You can create the string by doing: <?php $chars = '1234567890 ' . "'" . ';:|' . "'" . '"/\<>?][}{)(*&^%$#@!~abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; ?> Ken Link to comment https://forums.phpfreaks.com/topic/72200-create-str-with-special-characters/#findComment-365494 Share on other sites More sharing options...
AV1611 Posted October 9, 2007 Author Share Posted October 9, 2007 I guess I can't explain myself good enough. I have no way to predict what the string is, as I am dealing with a parsed text document. I know how to do this manually, as you did above with concanation. <sp?> What I need is for the script to do this automatically. I am dealing with csv/tsv file parsing between platforms. Link to comment https://forums.phpfreaks.com/topic/72200-create-str-with-special-characters/#findComment-365563 Share on other sites More sharing options...
AV1611 Posted October 9, 2007 Author Share Posted October 9, 2007 Sorry for double post TSV solves part of the problem but when you use .xls extension with tsv, if column has only numbers xls assumes it to be a number. we have part numbers like 0900.120 so tsv -> with .xls extension displays 900.12 in excel when opened. If I could put "0900.120" in the tsv, the it would be ok, but then if there is a " in the string, or \ for that matter, I'm screwed again. Link to comment https://forums.phpfreaks.com/topic/72200-create-str-with-special-characters/#findComment-365566 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.