zoukankan      html  css  js  c++  java
  • PHP函数

    1、strlen()函数返回字符串长度

    <?php
    echo strlen("hello")
    ?>

     2、对字符串中的单词计数

    <?php
    echo str_word_count("do you know this");
    ?>

     3、反转字符串

    <?php
    echo strrev("do you know this");
    ?>

     4、替换字符串中文本

    <?php
    echo str_replace("this","that","do you know this");
    ?>

     5、对数组排序函数

    sort() - 以升序对数组排序

    rsort() - 以降序对数组排序

    asort() - 根据值,以升序对关联数组进行排序

    ksort() - 根据键,以升序对关联数组进行排序

    arsort() - 根据值,以降序对关联数组进行排序

    krsort() - 根据键,以降序对关联数组进行排序

    例1:按字母升序/降序

    <?php
    $fruits=array("Banana","peach","apple");
    sort($fruits);
    $clength=count($fruits);
    for($x=0;$x<$clength;$x++)
    {
        echo $fruits[$x];
        echo "<br>";
    }
    ?>

    <?php
    $fruits=array("Banana","peach","apple");
    rsort($fruits);
    $clength=count($fruits);
    for($x=0;$x<$clength;$x++)
    {
        echo $fruits[$x];
        echo "<br>";
    }
    ?>

     例2:按数字升序/降序

    <?php
    $fruits=array(7,1,22,13);
    sort($fruits);
    $clength=count($fruits);
    for($x=0;$x<$clength;$x++)
    {
        echo $fruits[$x];
        echo "<br>";
    }
    ?>

    <?php
    $fruits=array(7,1,22,13);
    rsort($fruits);
    $clength=count($fruits);
    for($x=0;$x<$clength;$x++)
    {
        echo $fruits[$x];
        echo "<br>";
    }
    ?>

     例3:根据值对数组进行升序/降序

    <?php
    $age=array("ann"=>"30","tian"=>"18","bill"=>"26");
    asort($age);
    
    foreach($age as $x=>$x_value)
    {
        echo "key=" . $x . ", value=" . $x_value;
        echo "<br>";
    }
    ?>

    <?php
    $age=array("ann"=>"30","tian"=>"18","bill"=>"26");
    arsort($age);
    
    foreach($age as $x=>$x_value)
    {
        echo "key=" . $x . ", value=" . $x_value;
        echo "<br>";
    }
    ?>

     例4:根据键对数组进行升序/降序

    <?php
    $age=array("ann"=>"30","tian"=>"18","bill"=>"26");
    ksort($age);
    
    foreach($age as $x=>$x_value)
    {
        echo "key=" . $x . ", value=" . $x_value;
        echo "<br>";
    }
    ?>

    <?php
    $age=array("ann"=>"30","tian"=>"18","bill"=>"26");
    krsort($age);
    
    foreach($age as $x=>$x_value)
    {
        echo "key=" . $x . ", value=" . $x_value;
        echo "<br>";
    }
    ?>

  • 相关阅读:
    使用Mutex实现程序单实例运行(c#)
    KMP(转载来自Matrix67原创)
    【转载】搞ACM的你伤不起(转载,不过这个神作实在是太经典了)
    POJ 3125 Printer Queue【打印队列】
    弱校ACM奋斗史
    POJ 2063 Investment
    程序员的艺术:排序算法舞蹈【视频】
    POJ 2063 Investment【经典完全背包】
    快速幂模板
    搞ACM的你伤不起(转载,不过这个神作实在是太经典了)
  • 原文地址:https://www.cnblogs.com/tinkerbell/p/12733258.html
Copyright © 2011-2022 走看看