Jump to content


Photo

replacing special characters in php form


  • Please log in to reply
7 replies to this topic

#1 jbille

jbille
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 18 October 2006 - 10:24 PM

I was wondering how I could replace a period(.) and a dash(-) when passing them through a form to mysql.


#2 Caesar

Caesar
  • Members
  • PipPipPip
  • Advanced Member
  • 1,025 posts

Posted 18 October 2006 - 10:27 PM

<?php

str_replace(".","",$string);

?>

PHP Ninja

#3 jbille

jbille
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 18 October 2006 - 11:16 PM

What character besides "" can I replace it with so I can return it back to original form when outputting it.

#4 sanfly

sanfly
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationNew Zealand

Posted 18 October 2006 - 11:19 PM

Are you trying to replace them because your getting errors when you pass data containing those characters?  if so, check out addslashes() and stripslashes() in the PHP MANUAL
If you're not part of the solution, you're part of the precipitate

#5 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 18 October 2006 - 11:30 PM

Use this on all varables that select from the database.

$example=mysql_real_escape_string($example));

<?php
// Connect
$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password')
   OR die(mysql_error());

// Query
$query = sprintf("SELECT * FROM users WHERE user='%s' AND password='%s'",
           mysql_real_escape_string($user),
           mysql_real_escape_string($password));
?> 

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#6 slancasterfreak

slancasterfreak
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 18 October 2006 - 11:32 PM

you can replace it with anything unique like

<?php
$stringtostore=str_replace(".","perxx",$string);
?>

and then when you get it back out, replace it back with-

<?php
$gotstring=str_replace("perxx",".",$storedstring);
?>


#7 jbille

jbille
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 19 October 2006 - 12:02 AM

I am trying to let the user enter 123-456 in the form, and I want to load that number in a database.  However when I try this it just loads 123 in mysql and it stops at the -.  The same goes for 2.50, this puts 2 in the database.  And when I try to use $number=str_replace("-","000000", $number); it puts a false number into mysql.  I am using 000000 because $number is an INT(30).

#8 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 19 October 2006 - 12:07 AM

use varchar(30) then try ok.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users