Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


strip commas

Posted on 2005-04-20
Medium Priority
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, "http://www.myurl.com/form.php");

  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
  • 3
  • 2
LVL 14

Accepted Solution

cracky earned 200 total points
ID: 13830843
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

ID: 13831184
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

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

Expert Comment

ID: 13840278
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

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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

564 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