strip commas

Posted on 2005-04-20
Last Modified: 2011-08-18
I need to strip any commas from any of the fields that are being put into the database, here's the example code that I found to strip the commas, but I'm not sure where to put it to make sure that NO commas are sent into the DB...

$comment = str_replace(",","",$comment);

here's my code...


// define array_combine in PHP4
function &array_combine ( $keys, $values )
  if ( count($keys) != count($values) )
   return null;

  $keys = array_values($keys);
  $values = array_values($values);

  foreach( $keys as $index => $key )
     $newarray[$key] = $values[$index];

  return $newarray;
// end define array_combine

// post form to outside
  function post_it($datastream, $url) {
$url = preg_replace("@^http://@i", "", $url);
$host = substr($url, 0, strpos($url, "/"));
$uri = strstr($url, "/");
$reqbody = "";
      foreach($datastream as $key=>$val) {
          if (strlen($reqbody) > 1) $reqbody.= "&";
      $reqbody.= $key."=".urlencode($val);

$contentlength = strlen($reqbody);
     $reqheader =  "POST $uri HTTP/1.0\r\n".
                   "Host: $host\n". "User-Agent: PostIt\r\n".
     "Content-Type: application/x-www-form-urlencoded\r\n".
     "Content-Length: $contentlength\r\n\r\n".

$socket = fsockopen($host, 80, $errno, $errstr);

if (!$socket) {
   $result["errno"] = $errno;
   $result["errstr"] = $errstr;

fputs($socket, $reqheader);

while (!feof($socket)) {
   $result[] = fgets($socket, 4096);


$a = array('name','lastname','email','phone','eveningphone','cellphone','calltime','address','city','state','zip','debt','date', 'ref_id', 'Affiliate_ID', 'number');
$b = array($name,$lastname,$email,$phone,$eveningphone,$cellphone,$calltime,$address,$city,$state,$zip,$debt,$date,$ref_id,$Affiliate_ID,$number);
$data = array_combine($a, $b);

$result = post_it($data, "");

  if (isset($result["errno"])) {
    $errno = $result["errno"];
    $errstr = $result["errstr"];
    echo "<B>Error $errno</B> $errstr";
  } else {

    for($i=0;$i< count($result); $i++) echo $result[$i];


// end post form to outside

Question by:toddynho
    LVL 14

    Accepted Solution

    Make your own function:

    function strip_commas($value) {

      return str_replace(",","",$value);


    Then just encapsulate any variable you need commas stripped from with strip_commas()

    $reqbody = strip_commas($reqbody) for example.
    LVL 12

    Expert Comment

    Use like this ...
    You are collecting the data here to put into the DB,
    $first_name = str_replace(",","",$first_name);
    $last_name = str_replace(",","",$last_name);
    this is just before you put these via a query. (Need not to be the immediatly previous line nut before executing the query)

    then execute the query...
    insert into table (first_name,last_name) values ($first_name,$last_name);

    This will ensure that no , (comma) goes into the DB
    Hope  this helps. :)

    LVL 14

    Expert Comment

    No offence str_kani, but isn't that just a long-winded way of doing exactly what I suggested?
    LVL 12

    Expert Comment

    Ofcourse this is wider than your point. But I thought the user was confuesd about where to use. I didn't post it for getting some points, just to make it more clear.  

    You deserve full points :)  cracky!

    LVL 14

    Expert Comment

    No problem. I just wasn't sure whether you had read my comment, because they weren't all that different :)

    Featured Post

    Courses: Start Training Online With Pros, Today

    Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

    Join & Write a Comment

    Both Easy and Powerful How easy is PHP? (  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
    Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now