Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 132,462 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,360 people online right now. Registration is fast and FREE... Join Now!




Strings

 
Reply to this topicStart new topic

Strings

snoj
post 14 Mar, 2005 - 07:11 AM
Post #1


$Null

Group Icon
Joined: 31 Mar, 2003
Posts: 3,304



Thanked 5 times

Dream Kudos: 700
My Contributions


I am wondering. Is there a built in php function to find out has been single quoted or double quoted? I'm pretty sure that's what it's called anyway. dry.gif Or even convert one to the other.

CODE
<?php
$str1 = 'single quote';
$str2 = "double quote";
?>


I know a way I could do it, but I'd rather use a built in function then write my own.


[edit] Some spelling corrections.

This post has been edited by hotsnoj: 14 Mar, 2005 - 07:13 AM
User is offlineProfile CardPM

Go to the top of the page

cyberscribe
post 14 Mar, 2005 - 09:28 AM
Post #2


humble.genius

Group Icon
Joined: 5 May, 2002
Posts: 1,062



Thanked 2 times

Dream Kudos: 154
My Contributions


The difference between single and double quotes is whether or not PHP evaluates special characters and variables inside the quotes (double does, single doesn't). There is a difference in how PHP treats this and also in execution time. I don't know of any PHP functions that tell you about how PHP evaluated something. For this, you might need an analyzer or even to hook in to the Zend engine.
User is offlineProfile CardPM

Go to the top of the page

snoj
post 14 Mar, 2005 - 10:20 AM
Post #3


$Null

Group Icon
Joined: 31 Mar, 2003
Posts: 3,304



Thanked 5 times

Dream Kudos: 700
My Contributions


Thanks Cyber, though I knew that already. wink2.gif

For the caring:
CODE

<?php
/*
    string function str_parse(string $string, array $variables)
    
 Takes $string and parses as if it was double quoted.
 $variables must be an associative array. All keys must be able to
    be used as regular PHP varibles.
    
    Returns parsed $string.
    
    Usage:
 $varible = 'I\'m a single quoted string!';
 $string = '$variable';
 echo $string;
 //Prints $string as "$variable".
 
 $variables = array(
     'variable' => $variable);
 echo str_parse($string, $variables);
 //Prints $string as "I'm a single quoted string!".
*/
function str_parse($string, &$valArr) {
    //Save a md5 hash of the orginal string to check against.
    $string_back = md5($string);
    
    //Loop through our variable array.
    foreach($valArr as $key => $val) {
 $$key = $val;
    }
    //Check to see if we have the same string as before.
    //If we don't then it was a single quoted string.
    if(md5($string) == $string_back) {
 $i = 0;
 foreach($valArr as $key => $val) { $s[$i] = "\${$key}"; $r[$i++] = $val; }
 return str_replace($s, $r, $string);
    //Else it was and we can just return it.
    } else {
 return $string;
    }
}
?>
User is offlineProfile CardPM

Go to the top of the page

cyberscribe
post 14 Mar, 2005 - 02:11 PM
Post #4


humble.genius

Group Icon
Joined: 5 May, 2002
Posts: 1,062



Thanked 2 times

Dream Kudos: 154
My Contributions


Oh ... you don't want to find out how it was parsed ... you want to evaluate it. That's easy:

CODE

$foo = 'world';
$bar = 'Hello, $foo.';
eval("\$baz = \"$bar\";");
print $baz;


remember if you wrap this in a function, though, that you will not have access to variables declared outside the function unless you pass them in or make them global.
User is offlineProfile CardPM

Go to the top of the page

snoj
post 14 Mar, 2005 - 03:00 PM
Post #5


$Null

Group Icon
Joined: 31 Mar, 2003
Posts: 3,304



Thanked 5 times

Dream Kudos: 700
My Contributions


Thanks. Though I feel stupid because that's right on eval()'s doc page. sad.gif

I wonder if it would be a security risk though for what I'm doing....hmmm....
User is offlineProfile CardPM

Go to the top of the page

cyberscribe
post 14 Mar, 2005 - 11:16 PM
Post #6


humble.genius

Group Icon
Joined: 5 May, 2002
Posts: 1,062



Thanked 2 times

Dream Kudos: 154
My Contributions


If any of the eval'd variables are user defined, then:

YES
User is offlineProfile CardPM

Go to the top of the page

snoj
post 15 Mar, 2005 - 06:05 AM
Post #7


$Null

Group Icon
Joined: 31 Mar, 2003
Posts: 3,304



Thanked 5 times

Dream Kudos: 700
My Contributions


They most certainly are. otherwise I wouldn't have the original problem wink2.gif
User is offlineProfile CardPM

Go to the top of the page

cyberscribe
post 15 Mar, 2005 - 08:49 AM
Post #8


humble.genius

Group Icon
Joined: 5 May, 2002
Posts: 1,062



Thanked 2 times

Dream Kudos: 154
My Contributions


eek!
User is offlineProfile CardPM

Go to the top of the page

SpaceMan
post 17 Mar, 2005 - 07:39 PM
Post #9


D.I.C Regular

Group Icon
Joined: 20 Feb, 2003
Posts: 270

my shoe strings are different, maybe i should fix that.

(nifty info BTW)
User is offlineProfile CardPM

Go to the top of the page

NoVA_X
post 18 Mar, 2005 - 03:22 PM
Post #10


New D.I.C Head

*
Joined: 7 Mar, 2005
Posts: 6

They're not as large of a security risk if you're not using them in a query string. If you are, then they can manipulate data or use UNION in there.

For security in MySQL/PHP you should check out this SP Thread: http://www.sitepoint.com/forums/showthread.php?t=240473
User is offlineProfile CardPM

Go to the top of the page

cyberscribe
post 18 Mar, 2005 - 04:07 PM
Post #11


humble.genius

Group Icon
Joined: 5 May, 2002
Posts: 1,062



Thanked 2 times

Dream Kudos: 154
My Contributions


Actually, a call to eval() will evaluate whatever is in the string you send it -- which means it is a big problem even if you are not passing this to an sql query. You could cause all kinds of mischief with user-defined data inside an eval(). You can even overwrite existing variables inside the program whether or not register_globals is turned on.

Hence:

QUOTE

eek!
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/22/08 12:53PM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month