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.

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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