排序字符串对于以 PHP 语言所需的方式组织字符串很有用。字符串排序是使用 PHP 语言将给定字符串排序为所需格式的字符串方法。排序字符串是按照PHP技术中的排序函数对给定的排序字符串进行排列。排序字符串根据 Web 应用程序的要求对可用字符串进行分类和组装。排序字符串是按照 PHP 编码语言所需的升序或降序排列字符串。
广告 该类别中的热门课程 PHP 开发人员 - 专业化 | 8 门课程系列 | 3次模拟测试开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
对字符串进行排序的方法有很多种。下面是这些对字符串进行排序的方法。
字符串转换为数组并使用sort()方法。
$sortstring = 'sadycetfimlog';
$stringndarray = str_split($sortstring);
sort($stringndarray);
rsort($stringndarray);
$stringndarray = implode($stringndarray);
echo $stringndarray;
示例:升序或降序排序字符串示例和输出。
<!DOCTYPE html> <html> <body> <h3> Ascending Order of the Sort String </h3> <?php $sortstring = "sadycetfimlogb"; echo "given string is : <b>$sortstring </b><br/> "; $stringndarray = str_split($sortstring); sort($stringndarray); $stringndarray = implode($stringndarray); echo " sorting string in the ascending order: <b>$stringndarray</b><br/>"; ?> <h3> Descending Order of the Sort String </h3> <?php $sortstring = "bnhrzsadycetfimlog"; echo "given string is : <b>$sortstring </b><br/> "; $stringndarray = str_split($sortstring); rsort($stringndarray); $stringndarray = implode($stringndarray); echo " sorting string in the descending order: <b>$stringndarray</b>"; ?> </body> </html>
输出:
字符串交换位置并使用参数对字符串进行排序。
function sortStringphp (place arguments here…) {write code here…}
$sortstring = 'jhjabcdewyxdef';
$stringlength;
$currentposition;
function sortStringphp(&$sortstring, $stringlength, $currentposition=0) { write code here… }
if($currentposition == $stringlength){ return; }
$nextposition = $currentposition + 1;
while($nextposition< $stringlength){ if($sortstring[$nextposition] < $sortstring[$currentposition]){ $tempstring = $sortstring[$nextposition]; $sortstring[$nextposition] = $sortstring[$currentposition]; $sortstring[$currentposition] = $tempstring; } $nextposition++; }
sortStringphp($sortstring, $stringlength, $currentposition+1);
sortStringphp($sortstring,strlen($sortstring)); echo $sortstring;
示例:
<!DOCTYPE html> <html> <body> <h3> Ascending Order </h3> <?php $sortstring = 'iamgoodinthisplace'; echo "the given string : <b> $sortstring </b> <br/>"; $stringlength; $currentposition; function sortStringphp(&$sortstring, $stringlength, $currentposition=0) { $nextposition = $currentposition + 1; while($nextposition < $stringlength){ if($sortstring[$nextposition] < $sortstring[$currentposition]){ $tempstring = $sortstring[$nextposition]; $sortstring[$nextposition] = $sortstring[$currentposition]; $sortstring[$currentposition] = $tempstring; } $nextposition++; } if($currentposition == $stringlength){ return; } sortStringphp($sortstring, $stringlength, $currentposition+1); } sortStringphp($sortstring,strlen($sortstring)); echo " the sorted string : <b> $sortstring </b>"; ?> <h3> Descending Order </h3> <?php $sortstring1 = 'iamgoodinthisplace'; echo "the given string : <b> $sortstring1 </b> <br/>"; $stringlength1; $currentposition1; function sortStringphp1(&$sortstring1, $stringlength1, $currentposition1=0) { if($currentposition1 == $stringlength1) return $nextposition1 = $currentposition1 + 1; while($nextposition1 < $stringlength1){ if($sortstring1[$nextposition1] < $sortstring1[$currentposition1]){ $tempstring1 = $sortstring1[$nextposition1]; $sortstring1[$nextposition1] = $sortstring1[$currentposition1]; $sortstring1[$currentposition1] = $tempstring1; } $nextposition1++; } sortStringphp1($sortstring1, $stringlength1, $currentposition1+1); } sortStringphp1($sortstring1,strlen($sortstring1)); echo " the sorted string : <b> $sortstring1 </b>"; ?> </body> </html>
输出:
快速排序算法用于对字符串进行排序。
$stringleft = $stringright = '';
$stringlength = strlen($sortstring)-1 ;
if ($stringlength <= 0) { return $sortstring; }
$pivot = floor($stringlength/2);
do{ write sort string code here.. }while(sort string condition…)
if ($stringlength == $middlestring){ continue; }
if ($sortstring[$stringlength] >= $sortstring[$middlestring]) { $stringleft = $stringleft.$sortstring[$stringlength]; } else { $stringright = $stringright.$sortstring[$stringlength]; }
return sortStringphp($stringleft).$sortstring[$middlestring].sortStringphp($stringright);
$givenstring = sortStringphp ("goodtohaveacoffee"); echo " the sort string : <b>$givenstring</b>"
示例:
Ascending Order
$givenstring"; ?>Descending Order
$givenstring"; ?>
输出:
以上是PHP 字符串排序的详细内容。更多信息请关注PHP中文网其他相关文章!