More than one function on one variable

144 Views Asked by At

I want to write something in the database, but before I want to delete all spaces in the string and upper the first letter.

That's how I wanted to do it but it doesnt work :P

//INITIALIZATION OF VARIABLE $perfectFirstname 

    $perfectFirstname = str_replace(' ','',$_POST["firstnameInput"]);

//PART OF MY QUERY

    $sql .=     "'".mysql_real_escape_string(ucfirst($perfectFirstname))."', ";

How can I do this?

3

There are 3 best solutions below

1
On

You can use ucfirst for upper case:

$your_var = mysql_real_escape_string(ucfirst(strtolower(str_replace(' ','',$_POST["firstnameInput"]))));
0
On

how about,

$perfectFirstname = preg_replace('/\s+/', '', $_POST["firstnameInput"])
1
On
<?php

$_POST["firstnameInput"] = 'Billy';

$sql = '';
$sql .= "'".mysql_real_escape_string(ucfirst(str_replace(' ','', $_POST["firstnameInput"])))."', ";

var_dump($sql);


// OR, to reuse, a function like:

function MyEscape ($text) {
  return mysql_real_escape_string(ucfirst(str_replace(' ','', $text)));
}

$sql = '';
$sql .= "'".MyEscape($_POST["firstnameInput"])."', ";
var_dump($sql);

?>