dongxiai3003 2011-09-14 08:23
浏览 83
已采纳

将参数传递给php中的函数

I have some php code that is pretty much being duplicated save for some minor variable naming differences. How can I turn this into a reusable function where I can pass arguments thru?

This is the code which I am using twice. The second one is the same except all references of "affiliate" is changed to "social".

<?php
    $affiliate = wp_list_bookmarks( array( 'categorize' => 0, 'category' => '7', 'title_li' => '', 'orderby' => 'rating', 'show_images' => 0, 'echo' => 0 ) );
    preg_match_all( '/<li>.*?<\/li>/', $affiliate, $affiliate_matches );
    foreach ( $affiliate_matches[0] as $affiliate_match ) {
        preg_match( '/title=".*?"/', $affiliate_match, $affiliate_title );
        echo str_replace(
            $affiliate_title[0],
            $affiliate_title[0] . ' ' . strtolower( str_replace( array( 'title="', ' ' ), array( 'class="', '-' ), $affiliate_title[0] ) ),
            $affiliate_match
        ) . "
";
    }
?>

The other one is:

<?php
    $social = wp_list_bookmarks( array( 'categorize' => 0, 'category' => '2', 'title_li' => '', 'orderby' => 'rating', 'show_images' => 0, 'echo' => 0 ) );
    preg_match_all( '/<li>.*?<\/li>/', $social, $social_matches );
    foreach ( $social_matches[0] as $social_match ) {
        preg_match( '/title=".*?"/', $social_match, $social_title );
        echo str_replace(
            $social_title[0],
            $social_title[0] . ' ' . strtolower( str_replace( array( 'title="', ' ' ), array( 'class="', '-' ), $social_title[0] ) ),
            $social_match
        ) . "
";
    }
?>

I was thinking maybe I can call the function like

<?php links( array( 'affiliate', 7 ) ); ?>

or

<?php links( array( 'social', 2 ) ); ?>


Would combining them into a reusable function save processing time/resources or would it not matter?

  • 写回答

2条回答

  • dsk920417 2011-09-14 08:29
    关注

    the only thing that really changes is the category id so you only need to pass this to the function.

    function links($categoryId) {
            $affiliate = wp_list_bookmarks( array( 'categorize' => 0, 'category' => $categoryId, 'title_li' => '', 'orderby' => 'rating', 'show_images' => 0, 'echo' => 0 ) );
            preg_match_all( '/<li>.*?<\/li>/', $affiliate, $affiliate_matches );
            foreach ( $affiliate_matches[0] as $affiliate_match ) {
                preg_match( '/title=".*?"/', $affiliate_match, $affiliate_title );
                echo str_replace(
                    $affiliate_title[0],
                    $affiliate_title[0] . ' ' . strtolower( str_replace( array( 'title="', ' ' ), array( 'class="', '-' ), $affiliate_title[0] ) ),
                    $affiliate_match
                ) . "
    ";
            }
    
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制