Jump to content

Create $Str with special characters.


AV1611

Recommended Posts

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.

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...

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

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

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.