431,991 Members | 1,737 Online + Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,991 IT Pros & Developers. It's quick & easy.

# need to do a string replace of "asc" to "desc" or "desc" to "asc" first occurrence only

 P: n/a [PHP] \$orderBy = 's.app_date desc, s.last_name asc, s.first_name asc, s.mi asc'; if (\$_REQUEST['willDesc']) { \$ascArray = array('asc' => 'desc', 'desc' => 'asc'); // ARRAY OF ALL ORDERING POSSIBILITIES \$junk = preg_match('/([\s\t]+)a|[de]sc(,?.*\$)/i', \$orderBy, \$matchArray); \$orderBy = substr(\$orderBy, 0, strpos(\$orderBy, \$matchArray)) . ' ' . \$ascArray[\$matchArray] . substr(\$orderBy, strpos(\$orderBy, \$matchArray) + strlen(\$matchArray), strlen(\$orderBy)); } [/PHP] Basic premise: I have a SQL "ORDER BY" clause that will be configured like \$orderBy 's value. However, the very first occurrence of "desc" might instead be "asc". If the very first occurrence is "asc", it must become "desc"; likewise, if the very first occurrence is "desc", it must become "asc". I tried Regular Expressions but the pattern failed every single time to match and replace, so I gave up and tried a string function/RegExp code combination, also to no avail. I am not sure how to make this work so I need help figuring out how to do this. Thanx Phil Feb 3 '06 #1
5 Replies

 P: n/a comp.lang.php wrote: [PHP] \$orderBy = 's.app_date desc, s.last_name asc, s.first_name asc, s.mi asc'; if (\$_REQUEST['willDesc']) { \$ascArray = array('asc' => 'desc', 'desc' => 'asc'); // ARRAY OF ALL ORDERING POSSIBILITIES \$junk = preg_match('/([\s\t]+)a|[de]sc(,?.*\$)/i', \$orderBy, \$matchArray); \$orderBy = substr(\$orderBy, 0, strpos(\$orderBy, \$matchArray)) . ' ' . \$ascArray[\$matchArray] . substr(\$orderBy, strpos(\$orderBy, \$matchArray) + strlen(\$matchArray), strlen(\$orderBy)); } [/PHP] Basic premise: I have a SQL "ORDER BY" clause that will be configured like \$orderBy 's value. However, the very first occurrence of "desc" might instead be "asc". If the very first occurrence is "asc", it must become "desc"; likewise, if the very first occurrence is "desc", it must become "asc". I tried Regular Expressions but the pattern failed every single time to match and replace, so I gave up and tried a string function/RegExp code combination, also to no avail. I am not sure how to make this work so I need help figuring out how to do this. Thanx Phil function change_first_order_flag(\$orderBy){ \$tmp = explode( '__SPLIT__HERE__', preg_replace( '`(asc|desc)`i', '__SPLIT__HERE__\$1', \$orderBy ) ); if(count(\$tmp)>1){ // there are at least 2 elements // therefore, it was in there at least once if(substr(\$tmp,0,3)=='des'){ // it was in descending order \$tmp='as'.substr(\$tmp,3); }else{ // it was in ascending order \$tmp='des'.substr(\$tmp,3); } } return join(\$tmp); } -- Justin Koivisto, ZCE - ju****@koivi.com http://koivi.com Feb 3 '06 #2

 P: n/a Ok using your function: [PHP] if (!function_exists('change_first_order_flag')) { function change_first_order_flag(\$orderBy){ \$tmp = explode('__SPLIT__HERE__', preg_replace('`(asc|desc)`i', '__SPLIT__HERE__\$1', \$orderBy)); if (count(\$tmp) > 1) { // there are at least 2 elements // therefore, it was in there at least once if (substr(strtolower(\$tmp), 0, 3) == 'des') { // it was in descending order \$tmp = 'as' . substr(\$tmp, 3); } else { // it was in ascending order \$tmp = 'des' . substr(\$tmp, 3); } } return join(\$tmp); } } [/PHP] Produced the following MySQL query syntax error: Fatal error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'des, upper(s.student_firs' at line 1 using query: I'll keep looking at it but apparently it chops the ORDER BY clause incorrectly if the original ORDER BY clause contains ".. asc" Phil Justin Koivisto wrote: comp.lang.php wrote: [PHP] \$orderBy = 's.app_date desc, s.last_name asc, s.first_name asc, s.mi asc'; if (\$_REQUEST['willDesc']) { \$ascArray = array('asc' => 'desc', 'desc' => 'asc'); // ARRAY OF ALL ORDERING POSSIBILITIES \$junk = preg_match('/([\s\t]+)a|[de]sc(,?.*\$)/i', \$orderBy, \$matchArray); \$orderBy = substr(\$orderBy, 0, strpos(\$orderBy, \$matchArray)) . ' ' . \$ascArray[\$matchArray] . substr(\$orderBy, strpos(\$orderBy, \$matchArray) + strlen(\$matchArray), strlen(\$orderBy)); } [/PHP] Basic premise: I have a SQL "ORDER BY" clause that will be configured like \$orderBy 's value. However, the very first occurrence of "desc" might instead be "asc". If the very first occurrence is "asc", it must become "desc"; likewise, if the very first occurrence is "desc", it must become "asc". I tried Regular Expressions but the pattern failed every single time to match and replace, so I gave up and tried a string function/RegExp code combination, also to no avail. I am not sure how to make this work so I need help figuring out how to do this. Thanx Phil function change_first_order_flag(\$orderBy){ \$tmp = explode( '__SPLIT__HERE__', preg_replace( '`(asc|desc)`i', '__SPLIT__HERE__\$1', \$orderBy ) ); if(count(\$tmp)>1){ // there are at least 2 elements // therefore, it was in there at least once if(substr(\$tmp,0,3)=='des'){ // it was in descending order \$tmp='as'.substr(\$tmp,3); }else{ // it was in ascending order \$tmp='des'.substr(\$tmp,3); } } return join(\$tmp); } -- Justin Koivisto, ZCE - ju****@koivi.com http://koivi.com Feb 4 '06 #3

 P: n/a comp.lang.php wrote: Ok using your function: [PHP] if (!function_exists('change_first_order_flag')) { function change_first_order_flag(\$orderBy){ \$tmp = explode('__SPLIT__HERE__', preg_replace('`(asc|desc)`i', '__SPLIT__HERE__\$1', \$orderBy)); if (count(\$tmp) > 1) { // there are at least 2 elements // therefore, it was in there at least once if (substr(strtolower(\$tmp), 0, 3) == 'des') { // it was in descending order \$tmp = 'as' . substr(\$tmp, 3); } else { // it was in ascending order \$tmp = 'des' . substr(\$tmp, 3); } } return join(\$tmp); } } [/PHP] Produced the following MySQL query syntax error: > Fatal error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'des, upper(s.student_firs' at line 1 using query: I'll keep looking at it but apparently it chops the ORDER BY clause incorrectly if the original ORDER BY clause contains ".. asc" Phil Change: \$tmp = 'des' . substr(\$tmp, 3); To: \$tmp = 'des' . substr(\$tmp, 2); That should do it... I was in a hurry to get out of the office, so i didn't test it. Feb 4 '06 #4

 P: n/a Much thanx! That worked!! Of course, I did a slight variation: \$tmp = 'desc' . substr(\$tmp, 3); Phil Justin Koivisto wrote: comp.lang.php wrote: Ok using your function: [PHP] if (!function_exists('change_first_order_flag')) { function change_first_order_flag(\$orderBy){ \$tmp = explode('__SPLIT__HERE__', preg_replace('`(asc|desc)`i', '__SPLIT__HERE__\$1', \$orderBy)); if (count(\$tmp) > 1) { // there are at least 2 elements // therefore, it was in there at least once if (substr(strtolower(\$tmp), 0, 3) == 'des') { // it was in descending order \$tmp = 'as' . substr(\$tmp, 3); } else { // it was in ascending order \$tmp = 'des' . substr(\$tmp, 3); } } return join(\$tmp); } } [/PHP] Produced the following MySQL query syntax error: > > Fatal error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'des, upper(s.student_firs' at line 1 using query: I'll keep looking at it but apparently it chops the ORDER BY clause incorrectly if the original ORDER BY clause contains ".. asc" Phil Change: \$tmp = 'des' . substr(\$tmp, 3); To: \$tmp = 'des' . substr(\$tmp, 2); That should do it... I was in a hurry to get out of the office, so i didn't test it. Feb 5 '06 #5

 P: n/a "comp.lang.php" wrote in message news:11**********************@z14g2000cwz.googlegr oups.com... [PHP] \$orderBy = 's.app_date desc, s.last_name asc, s.first_name asc, s.mi asc'; if (\$_REQUEST['willDesc']) { \$ascArray = array('asc' => 'desc', 'desc' => 'asc'); // ARRAY OF ALL ORDERING POSSIBILITIES \$junk = preg_match('/([\s\t]+)a|[de]sc(,?.*\$)/i', \$orderBy, \$matchArray); \$orderBy = substr(\$orderBy, 0, strpos(\$orderBy, \$matchArray)) . ' ' . \$ascArray[\$matchArray] . substr(\$orderBy, strpos(\$orderBy, \$matchArray) + strlen(\$matchArray), strlen(\$orderBy)); } [/PHP] Basic premise: I have a SQL "ORDER BY" clause that will be configured like \$orderBy 's value. However, the very first occurrence of "desc" might instead be "asc". If the very first occurrence is "asc", it must become "desc"; likewise, if the very first occurrence is "desc", it must become "asc". I tried Regular Expressions but the pattern failed every single time to match and replace, so I gave up and tried a string function/RegExp code combination, also to no avail. I am not sure how to make this work so I need help figuring out how to do this. Thanx Phil this function should work as long as asc and desc are not in the column names, and there is only 1 instance of asc or desc. Feb 17 '06 #6

### This discussion thread is closed

Replies have been disabled for this discussion.
{"ft_lang_label":"__label__en","ft_lang_prob":0.69900185,"math_prob":0.6787451,"size":9882,"snap":"2019-35-2019-39","text_gpt3_token_len":2936,"char_repetition_ratio":0.12198826,"word_repetition_ratio":0.83435166,"special_character_ratio":0.3374823,"punctuation_ratio":0.19167523,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.95155364,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-09-18T18:45:15Z\",\"WARC-Record-ID\":\"<urn:uuid:b5ff7e57-1f0d-4512-bd22-be32f6c0a739>\",\"Content-Length\":\"43268\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:4bf539fc-94bd-4285-a971-3b6c3c9eddb4>\",\"WARC-Concurrent-To\":\"<urn:uuid:4f96c372-6a4d-4df2-867b-ed3abea68dee>\",\"WARC-IP-Address\":\"104.197.32.212\",\"WARC-Target-URI\":\"https://bytes.com/topic/php/answers/452949-need-do-string-replace-asc-desc-desc-asc-first-occurrence-only\",\"WARC-Payload-Digest\":\"sha1:HSWQ56F2MO4AUMY4UBB6YQQX4LQCYYVI\",\"WARC-Block-Digest\":\"sha1:C5SC5DJSGGSDCI2UYRBJT237DMYUKCF7\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-39/CC-MAIN-2019-39_segments_1568514573323.60_warc_CC-MAIN-20190918172932-20190918194932-00551.warc.gz\"}"}