You are here: Re: need to do a string replace of "asc" to "desc" or "desc" to "asc" first occurrence only « PHP Programming Language « IT news, forums, messages
Re: need to do a string replace of "asc" to "desc" or "desc" to "asc" first occurrence only

Posted by Jim Michaels on 02/17/06 04:22

"comp.lang.php" <phillip.s.powell@gmail.com> wrote in message
news:1139006555.477380.219680@z14g2000cwz.googlegroups.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[1])) .
> ' ' . $ascArray[$matchArray[1]] .
> substr($orderBy, strpos($orderBy, $matchArray[1]) +
> strlen($matchArray[1]), 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.

<?php
function change_first_order_flag($orderBy){
$o=$orderBy;
$start=0;
do {
$a=stripos($o, "order by", $start);
if ($a===false) {} else {
$a+=strlen("order by ");
$start=$a;
$ascpos=stripos($o, "ASC", $a);
$descpos=stripos($o, "DESC", $a);
if ($ascpos===false) {
if ($descpos===false) {
//nothing to modify
} else { //desc found
$o=substr($o, 0, $descpos-1) . "ASC" . substr($o,
$descpos+strlen("DESC"));
$start=$a+strlen("ASC");
}
} else {
if ($descpos===false) {
$o=substr($o, 0, $ascpos-1) . "DESC" . substr($o,
$ascpos+strlen("ASC"));
$start=$a+strlen("DESC");
} else {
//impossible situation! both ASC and DESC found!
print "error: both ASC and DESC found in ORDER BY
statement"
}
}
//find another column in the ORDER BY series
$c=strpos($o, ",", $start);
if ($c===false) {
break;
} else {
$start=$c+1;
}
}
} while (true);
return $o;
}
?>

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация