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,june";
What I want to end up with is:
$myList = "'michael','frank','peter','sally','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 7 16423
On May 26, 6:48 am, Michael Sharman <sha...@gmail.comwrote:
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,june";
What I want to end up with is:
$myList = "'michael','frank','peter','sally','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($myList, 'addWrapper', "'");
print_r($myList);
?>
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,june";
What I want to end up with is:
$myList = "'michael','frank','peter','sally','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.
..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,june";
What I want to end up with is:
$myList = "'michael','frank','peter','sally','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
On May 26, 9:14 pm, Michael Fesser <neti...@gmx.dewrote:
.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,june";
What I want to end up with is:
$myList = "'michael','frank','peter','sally','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.
Michael Sharman wrote:
On May 26, 9:14 pm, Michael Fesser <neti...@gmx.dewrote:
>.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,june"; What I want to end up with is: $myList = "'michael','frank','peter','sally','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.
On May 27, 4:50 am, sheldonlg <sheldonlgwrote:
Michael Sharman wrote:
On May 26, 9:14 pm, Michael Fesser <neti...@gmx.dewrote:
.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,june"; What I want to end up with is: $myList = "'michael','frank','peter','sally','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
On May 26, 3:48 am, Michael Sharman <sha...@gmail.comwrote:
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,june";
What I want to end up with is:
$myList = "'michael','frank','peter','sally','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,june";
// =====================================
// = First Method, Somewhat Functional =
// =====================================
$my_new_list = implode(
',',
array_map(
create_function(
'$a',
'return "\'"
. mysql_real_escape_string($a)
. "\'";'
),
explode(',', $my_list)
)
);
// =============================
// = Second Method, With Regex =
// =============================
$my_newer_list = preg_replace_callback(
'/(^|,)([^,]+)/',
create_function(
'$matches',
'return "$matches[1]\'"
. mysql_real_escape_string($matches[2])
. "\'";'
),
$my_list
);
// ==========
// = Result =
// ==========
echo $my_new_list, "\n", $my_newer_list;
?>
jt This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Tim |
last post by:
I have block of code adding values to dropdown list under
if (!Page.IsPostBack)
{
adding items
}
else
{
refreshing data
}
|
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...
|
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...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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...
|
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...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |