Hi I have the list of all countries in this format: ""=>Armenia, ""=>Aruba, ""=>Australia, ""=>Austria, ""=>Azerbaijan, ""=>Bahamas, ""=>Bahrain, ""=>Bangladesh, ""=>Barbados, I want to make it in the following format: (copy name between ""): "Afghanistan"=>Afghanistan, "Albania"=>Albania, "Algeria"=>Albania, "Andorra"=>Andorra, "Angola"=>Angola, "Anguilla"=>Anguilla, "Antigua an d Barbuda"=>Antigua an d Barbuda, "Argentina"=>Argentina, As the number of countries are a lot, how can i write a function to do the job for me? thanks for your help
Using JavaScript -- use the indexOf function to search between the ">" and "," ... save the results and concatenate the results to the front of the string replacing the ""=. Sorry just noticed this was the PHP forum--- use the strstr function instead of the Javascript indexOf function-- see php.net. Q...
what u mean by "Albania"=>Albania' is making it an array that has key "Albania" and value Albania? or u want it as a string. And, You want the script to process the list of countries (that you have now) only when you need it : makeArray() or you want to process the list of countries that you have now and print it as an array so that you can paste in your file? : makeArray_for_copy() kinda confusing sentences.. EDIT: anyway test it using these simple functions. i assume the country list you have is a string. If it is, just make is as a variable like what i did. $lol = '""=>Armenia, ""=>Aruba, ""=>Australia, ""=>Austria, ""=>Azerbaijan, ""=>Bahamas, ""=>Bahrain, ""=>Bangladesh, ""=>Barbados, '; function makeArray($theString) { $lol = str_replace('""=>','',$theString); $lol = str_replace("\r\n",'',$lol); $lol = explode(',',$lol); foreach($lol as $lolx) { if (!empty($lolx)) { $lol_db[$lolx] = $lolx; } } return $lol_db; } function makeArray_for_copy($theString) { $lol = str_replace('""=>','',$theString); $lol = str_replace("\r\n",'',$lol); $lol = explode(',',$lol); foreach($lol as $lolx) { if (!empty($lolx)) { $lol_string .= '"'.$lolx.'" => \''.$lolx.'\',<br />'; } } return $lol_string; } print '<pre>'; print '<b>makeArray()</b><br />'; // test the makeArray print_r (makeArray($lol)); print '<br /><br /><b>makeArray_for_copy()</b><br />'; // test the makeArray_for_copy print makeArray_for_copy($lol); print '</pre>'; PHP:
$list = '""=>Armenia, ""=>Aruba, ""=>Australia, ""=>Austria, ""=>Azerbaijan, ""=>Bahamas, ""=>Bahrain, ""=>Bangladesh, ""=>Barbados, '; $pattern = '/""=>([^,]+),/'; $output = preg_replace($pattern,'"$1"=>$1,',$list); PHP: (it's a bit simpler than the above)