473,801 Members | 2,425 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Adding single quotes around items in a list

Is there a PHP function which can add single quotes around each item
in a list?

So if I have:

$myList = "michael,frank, peter,sally,jun e";

What I want to end up with is:

$myList = "'michael','fra nk','peter','sa lly','june'";

I know I can loop over the list and do it manually, I was wondering if
there was a cool function to do it automatically.

I want to add this to the "IN" part of a database "WHERE" clause.

Thanks in advance

Jun 2 '08 #1
7 16450
On May 26, 6:48 am, Michael Sharman <sha...@gmail.c omwrote:
Is there a PHP function which can add single quotes around each item
in a list?

So if I have:

$myList = "michael,frank, peter,sally,jun e";

What I want to end up with is:

$myList = "'michael','fra nk','peter','sa lly','june'";

I know I can loop over the list and do it manually, I was wondering if
there was a cool function to do it automatically.

I want to add this to the "IN" part of a database "WHERE" clause.

Thanks in advance
Use array_walk and implement the change in a callback function.
http://us3.php.net/manual/en/function.array-walk.php
<?php

//modified $myList to make it an actual array
$myList = array("michael" ,"frank","peter ","sally","june ");

function addWrapper (&$value, $key, $wrapper) {
$value = $wrapper.$value .$wrapper;
//no return, passed by reference
}

array_walk($myL ist, 'addWrapper', "'");
print_r($myList );
?>
Jun 2 '08 #2
Michael Sharman wrote:
Is there a PHP function which can add single quotes around each item
in a list?

So if I have:

$myList = "michael,frank, peter,sally,jun e";

What I want to end up with is:

$myList = "'michael','fra nk','peter','sa lly','june'";

I know I can loop over the list and do it manually, I was wondering if
there was a cool function to do it automatically.

I want to add this to the "IN" part of a database "WHERE" clause.

Thanks in advance

I haven't tested it, but you might try this:

IN "('" . str_replace("," , "','", $myList) . "')"

for the IN part of the WHERE clause.
Jun 2 '08 #3
..oO(Michael Sharman)
>Is there a PHP function which can add single quotes around each item
in a list?

So if I have:

$myList = "michael,frank, peter,sally,jun e";

What I want to end up with is:

$myList = "'michael','fra nk','peter','sa lly','june'";

I know I can loop over the list and do it manually, I was wondering if
there was a cool function to do it automatically.
You could replace the commas with ',' or use a combination of explode()
and implode():

$myList = "'".str_replace (",", "','", $myList)."'";

or

$myList = "'".implode("', '", explode(",", $myList))."'";

HTH
Micha
Jun 2 '08 #4
On May 26, 9:14 pm, Michael Fesser <neti...@gmx.de wrote:
.oO(Michael Sharman)
Is there a PHP function which can add single quotes around each item
in a list?
So if I have:
$myList = "michael,frank, peter,sally,jun e";
What I want to end up with is:
$myList = "'michael','fra nk','peter','sa lly','june'";
I know I can loop over the list and do it manually, I was wondering if
there was a cool function to do it automatically.

You could replace the commas with ',' or use a combination of explode()
and implode():

$myList = "'".str_replace (",", "','", $myList)."'";

or

$myList = "'".implode("', '", explode(",", $myList))."'";

HTH
Micha
Thanks all for the great ideas, I ended up going with:

IN('" . str_replace("," , "','", $myList) . "')"

That was the simplest for what I was doing.
Jun 2 '08 #5
Michael Sharman wrote:
On May 26, 9:14 pm, Michael Fesser <neti...@gmx.de wrote:
>.oO(Michael Sharman)
>>Is there a PHP function which can add single quotes around each item
in a list?
So if I have:
$myList = "michael,frank, peter,sally,jun e";
What I want to end up with is:
$myList = "'michael','fra nk','peter','sa lly','june'";
I know I can loop over the list and do it manually, I was wondering if
there was a cool function to do it automatically.
You could replace the commas with ',' or use a combination of explode()
and implode():

$myList = "'".str_replace (",", "','", $myList)."'";

or

$myList = "'".implode("', '", explode(",", $myList))."'";

HTH
Micha

Thanks all for the great ideas, I ended up going with:

IN('" . str_replace("," , "','", $myList) . "')"

That was the simplest for what I was doing.

I assume there was a " before the IN or a " followed by the previous
part of the query before the IN.
Jun 2 '08 #6
On May 27, 4:50 am, sheldonlg <sheldonlgwrote :
Michael Sharman wrote:
On May 26, 9:14 pm, Michael Fesser <neti...@gmx.de wrote:
.oO(Michael Sharman)
>Is there a PHP function which can add single quotes around each item
in a list?
So if I have:
$myList = "michael,frank, peter,sally,jun e";
What I want to end up with is:
$myList = "'michael','fra nk','peter','sa lly','june'";
I know I can loop over the list and do it manually, I was wondering if
there was a cool function to do it automatically.
You could replace the commas with ',' or use a combination of explode()
and implode():
$myList = "'".str_replace (",", "','", $myList)."'";
or
$myList = "'".implode("', '", explode(",", $myList))."'";
HTH
Micha
Thanks all for the great ideas, I ended up going with:
IN('" . str_replace("," , "','", $myList) . "')"
That was the simplest for what I was doing.

I assume there was a " before the IN or a " followed by the previous
part of the query before the IN.
There was, sorry...I should have posted the complete source.

Thanks sheldonlg
Jun 2 '08 #7
On May 26, 3:48 am, Michael Sharman <sha...@gmail.c omwrote:
Is there a PHP function which can add single quotes around each item
in a list?

So if I have:

$myList = "michael,frank, peter,sally,jun e";

What I want to end up with is:

$myList = "'michael','fra nk','peter','sa lly','june'";

I know I can loop over the list and do it manually, I was wondering if
there was a cool function to do it automatically.

I want to add this to the "IN" part of a database "WHERE" clause.

Thanks in advance
This is a bit late, but...

If $my_list is tainted (i.e. it originated from the user) you'd be
wise to escape the list items. I've attached two ways of doing this.
The first uses implode/explode and array_map (I call this the
"functional " approach) and the second uses preg_replace with a
callback.

<?php

$my_list = "michael,frank, peter,sally,jun e";

// =============== =============== =======
// = First Method, Somewhat Functional =
// =============== =============== =======

$my_new_list = implode(
',',
array_map(
create_function (
'$a',
'return "\'"
. mysql_real_esca pe_string($a)
. "\'";'
),
explode(',', $my_list)
)
);

// =============== ==============
// = Second Method, With Regex =
// =============== ==============

$my_newer_list = preg_replace_ca llback(
'/(^|,)([^,]+)/',
create_function (
'$matches',
'return "$matches[1]\'"
. mysql_real_esca pe_string($matc hes[2])
. "\'";'
),
$my_list
);

// ==========
// = Result =
// ==========

echo $my_new_list, "\n", $my_newer_list;

?>

jt
Jun 2 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
36186
by: Jakanapes | last post by:
Hi all, I'm looking for a way to scan a block of text and replace all the double quotes (") with single quotes ('). I'm using PHP to pull text out of a mySQL table and then feed the text into a javascript function called by onClick. The problem is that the text may contain single or double quotes, which screws up the javascript. I eliminated the single quote problem by running the text through addslashes, but the double quotes are a...
3
2080
by: Rock | last post by:
Hi, I started using a python based screen scraper called newsscraper I downloaded from sourceforge. http://sourceforge.net/projects/newsscraper/. I have created many python templates that work just fine from their examples however I ran into a road block with sites that use single quotes instead of double quotes for specifying url in their web pages. For example: <a href='http://www.foo/'> instead of the usual
7
3986
by: Brian van den Broek | last post by:
Hi all, I'm posting partly so my problem and solution might be more easily found by google, and partly out of mere curiosity. I've just spent a frustrating bit of time figuring out why pydoc didn't extract a description from my module docstrings. Even though I had a well formed docstring (one line, followed by a blank line, followed by the rest of the docstring), when I ran Module docs, my modules showed up as having "no description"....
1
6948
by: billmiami2 | last post by:
I'm trying to pass through a SQL statement to an Oracle database using OPENROWSET. My problem is that I'm not sure of the exact syntax I need to use when the SQL statement itself contains single quotes. Unfortunately, OPENROWSET doesn't allow me to use parameters so I can't get around the problem by assigning the SQL statement to a parameter of type varchar or nvarchar as in SELECT * FROM...
3
3104
by: Jason | last post by:
I have several tables with quite a few fields and I'm getting errors when trying to insert records with single quotes in the data like: name = John O'Henry or a city name of O'Fallen So I went ahead and added a replace to replace the ' with " but now other fields are having the same problem and there are multiple fields involved. This data gets into having a lot of symbols used, etc. So rather than go through and replace these quotes...
3
2769
by: Richard Hollenbeck | last post by:
I have an email field and a command button to send email. The problem is that when the email program opens up there are apostrophes around the email address that I have to manually remove before sending the email. My code is very simple. I can't understand why the apostrophes are put there but the addresses won't work until they are stipped of the apostrophes. Here's my code: Private Sub cmdEmail_Click() If len(txtEmail) > 0 Then
5
2922
by: Tim | last post by:
I have block of code adding values to dropdown list under if (!Page.IsPostBack) { adding items } else { refreshing data }
7
21035
by: gar | last post by:
Hi, I need to replace all the double quotes (") in a textbox with single quotes ('). I used this code text= Replace(text, """", "'" This works fine (for normal double quotes).The problem comes in when you copy a double quote from MS Word and paste it in the text box. What happens is the double quote becomes slanted (“) so my code above can't filter it. I tried to do this text= Replace(text, "““","'")
7
10544
by: nick.bonadies | last post by:
I'm trying to deal with user inputs of single quotes into form fields that get input into a MSSQL database. So far I have discovered that if I turn on magic_quotes_sybase in my php.ini file PHP will correctly escape the single quotes. The problem happens when I am trying to retrieve data from the database, PHP will try to comment out what it has already commented out, instead of stripping the extra single quote. So as an example, if...
0
9698
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9556
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10521
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10295
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7593
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5486
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5618
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3782
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2961
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.