how to ignore single qoutes in side string

  • Usman
  • Born
  • Born
  • Usman
  • Posts: 1
  • Loc: Lahore

Post 3+ Months Ago

HI there guys..
well plzz help me i got a problem .. i have a string like
$str='hello i m usman my designation is 'software engg''
means i have single qoutes two time in a string how can i ignore single qoutes from a string
please reply ASAP
  • rtm223
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1855
  • Loc: Uk

Post 3+ Months Ago

method one, use different types of quotes:
Code: [ Select ]
$str="hello i m usman my designation is 'software engg'"


method 2:use escape characters

Code: [ Select ]
$str='hello i m usman my designation is \'software engg\''

(backslash makes the following character act differently, in this case - it is used as normal text.
  • Sabu
  • Beginner
  • Beginner
  • Sabu
  • Posts: 47

Post 3+ Months Ago

The function

Code: [ Select ]
$str = addslashes($str);


Will take any string you give it and do the whole \' thing. Not exactly what you needed, it appears, but good for reference purposes if anyone searches up this thread again
  • rtm223
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1855
  • Loc: Uk

Post 3+ Months Ago

addslashes() is only really useful for escaping characters to prevent SQL injections isn't it? If you have already created the string in code, you would not need to add the slashes as the quotes would already be there. And if you haven't made the string already, then you don't need addslashes.

Forgive me if I'm wrong - very very tired here :lol:


For incoming variables I use something like this:
PHP Code: [ Select ]
 
$_GET['varName'] = CF_addslashes($_GET['varName']);
 
 
 
function CF_addslashes($stringArg){
 
    if(!get_magic_quotes_gpc()){
 
        return addslashes($stringArg);
 
    }else{
 
        return $stringArg;
 
    }
 
}
 
 
  1.  
  2. $_GET['varName'] = CF_addslashes($_GET['varName']);
  3.  
  4.  
  5.  
  6. function CF_addslashes($stringArg){
  7.  
  8.     if(!get_magic_quotes_gpc()){
  9.  
  10.         return addslashes($stringArg);
  11.  
  12.     }else{
  13.  
  14.         return $stringArg;
  15.  
  16.     }
  17.  
  18. }
  19.  
  20.  


Thats actually nothing like what I use, but the code is at home and I cannot remember the if/then/else shortcut. The principle is the same.

Also, I don't know what the CF_ means - I have used that whenever I make a modified version of an existing function for so long I just cannot even remember what it means now!
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

Just use double quotes for the string. I have never used singel quotes because if you have a variable inside the string it doesn't print the value of the variable

PHP Code: [ Select ]
 
$variable      = "single quote's heaven"
 
$str              = "this is a string with a $variable inside it"
 
//ouput: this is a string with a single quote's heaven inside it
 
 
 
$variable      = 'single quote\'s heaven'
 
$str              = 'this is a string with a $variable inside it'
 
//output: this is a string with a $variable inside it
 
 
  1.  
  2. $variable      = "single quote's heaven"
  3.  
  4. $str              = "this is a string with a $variable inside it"
  5.  
  6. //ouput: this is a string with a single quote's heaven inside it
  7.  
  8.  
  9.  
  10. $variable      = 'single quote\'s heaven'
  11.  
  12. $str              = 'this is a string with a $variable inside it'
  13.  
  14. //output: this is a string with a $variable inside it
  15.  
  16.  


Don't confuse the way PHP and ASP deal with quotes. PHP does it alot more gracefully than just doubling all the quotes. In ASP you are required to double them the way you have (because the ASP engine merely counts the number of instances in the string - vomit).

// Edit Deadlines make the brain numb thanks for pointing that out
  • s15199d
  • Expert
  • Expert
  • User avatar
  • Posts: 524
  • Loc: NC, USA

Post 3+ Months Ago

not to correct Rabid...but for clarification for anyone researching this topic...rabid's first output should read....
this is a string with a single quote's heaven inside it
  • Carnix
  • Guru
  • Guru
  • User avatar
  • Posts: 1098

Post 3+ Months Ago

This is the best way, within PHP to do specifically what you are asking:

rtm223 wrote:
PHP Code: [ Select ]
$str="hello i m usman my designation is 'software engg'"



to you the addslashes(); function, you'd still have to either do the above, or escape the slashes, since you'd get a parse error (unterminated string) if you didn't.

*EDIT: Oh.. and I bet CF_ stood for "Custom Function"... just a hunch =]
Hope it didn't stand for Coldfusion! EEEK!

.c

Post Information

  • Total Posts in this topic: 7 posts
  • Users browsing this forum: No registered users and 47 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.