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>";
    }
    ?>

  • 相关阅读:
    批处理 bat 查询局域网内在线电脑IP
    svn忽略不需要同步的文件夹或文件
    脚本设置IP bat 命令行设置自动获取IP和固定IP
    Lua中使用状态机FSM简单例子
    Lua 数组排序 table.sort的注意事项
    lua中怎么替换掉字符串中的$^特殊字符?
    C#应用程序隐藏调用bat脚本
    Oracle 基础教程
    Python编程
    利用Python进行数据分析(九)NumPy高级应用
  • 原文地址:https://www.cnblogs.com/tinkerbell/p/12733258.html
Copyright © 2011-2022 走看看