Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Surround string with quotes

Tags:

string

php

Is there a function in PHP that adds quotes to a string?

like "'".str."'"

This is for a sql query with varchars. I searched a little, without result...

I do the following:

$id = "NULL";
$company_name = $_POST['company_name'];         
$country = $_POST['country'];
$chat_language = $_POST['chat_language'];
$contact_firstname = $_POST['contact_firstname'];
$contact_lastname = $_POST['contact_lastname'];
$email = $_POST['email'];
$tel_fix = $_POST['tel_fix'];
$tel_mob = $_POST['tel_mob'];       
$address = $_POST['address'];       
$rating = $_POST['rating'];

$company_name = "'".mysql_real_escape_string(stripslashes($company_name))."'";
$country = "'".mysql_real_escape_string(stripslashes($country))."'";
$chat_language = "'".mysql_real_escape_string(stripslashes($chat_language))."'";
$contact_firstname = "'".mysql_real_escape_string(stripslashes($contact_firstname))."'";
$contact_lastname = "'".mysql_real_escape_string(stripslashes($contact_lastname))."'";
$email = "'".mysql_real_escape_string(stripslashes($email))."'";
$tel_fix = "'".mysql_real_escape_string(stripslashes($tel_fix))."'";
$tel_mob = "'".mysql_real_escape_string(stripslashes($tel_mob))."'";
$address = "'".mysql_real_escape_string(stripslashes($address))."'";
$rating = mysql_real_escape_string(stripslashes($rating));

$array = array($id, $company_name, $country, $chat_language, $contact_firstname, 
$contact_lastname, $email, $tel_fix, $tel_mob, $address, $rating);
$values = implode(", ", $array);

$query = "insert into COMPANIES values(".$values.");";
like image 741
serhio Avatar asked Feb 21 '10 00:02

serhio


People also ask

What is a string with quotation marks called?

A series of characters enclosed in matching quotation marks is called a literal string. The following examples both contain literal strings. SAY 'This is a REXX literal string.' /* Using single quotes */ SAY "This is a REXX literal string."

Can you have a string with a quote inside it?

Sometimes you might want to place quotation marks (" ") in a string of text. For example: She said, "You deserve a treat!" As an alternative, you can also use the Quote field as a constant.

What do you surround a quote with?

In American English, use double quotation marks to surround a quotation. In British English, you can use single or double quotation marks for that.

Can string be enclosed in single quotes?

That means a string enclosed in the single quote is equal to the same string enclosed in the double quote provided that necessary characters are escaped.


2 Answers

Create your own.

function addQuotes($str){
    return "'$str'";
}
like image 61
Phoexo Avatar answered Oct 08 '22 21:10

Phoexo


Thought I'd contribute an option that answers the question of "Is there a function in PHP that adds quotes to a string?" - yes, you can use str_pad(), although it's probably easier to do it manually.

Benefits of doing it with this function are that you could also pass a character to wrap around the variable natively within PHP:

function str_wrap($string = '', $char = '"')
{
    return str_pad($string, strlen($string) + 2, $char, STR_PAD_BOTH);
}

echo str_wrap('hello world');      // "hello world"
echo str_wrap('hello world', '@'); // @hello world@
like image 31
scrowler Avatar answered Oct 08 '22 20:10

scrowler