Sometimes we need to add quotes to values in a string separated by comma. For example while using MySQL FULLTEXT search, we need to use some kind of string like ‘pasta’,’ tuna’,’ eggs’ OR finding big list of Ids from MySQL such as WHERE item IN (‘pasta’,’ tuna’,’ eggs’). Here in this post, you have running PHP code to add quotes to values in a string separated by a comma using PHP.
Also, read:
- 15+ regular expressions for PHP developers
- Convert Associative Array into XML in PHP
- Convert XML into Associative Array in PHP
- Converting an Array to JSON in PHP
- Converting JSON to Array or Object in PHP
<?php $mystring ="pasta, tuna, eggs"; $result_string = "'" . str_replace(",", "','", $mystring) . "'"; echo "Input String: ".$mystring; echo “Output String: ".$result_string; ?>
Input String: pasta, tuna, eggs
Output String: ‘pasta’,’ tuna’,’ eggs’
You may also like:
- Working with php.ini file Configuration
- Control Statements in PHP
- Convert Associative Array into XML in PHP
- Convert XML into Associative Array in PHP
- Using Prepared Statement with PHP & MySQL
- How to Upload File in PHP
- Converting an Array to JSON in PHP
- Converting JSON to Array or Object in PHP
- Manipulating PHP arrays: push, pop, shift, unshift
- Remove Repeated Words From String in PHP
- Converting a PHP Array to a Query String
- 15+ regular expressions for PHP developers
- 10 Most Important Directory Functions in PHP
- 10 little known but useful PHP functions
- PHP Script to Download Large Files Reliably