我有一个几乎被复制保存一些小的变量命名的差别一些PHP代码。 我怎样才能变成一个可重复使用的功能,我可以传递参数通过这个?
这是我使用两倍的代码。 第二个是除的“关联方”的所有引用相同更改为“社会人”。
<?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
) . "\n";
}
?>
另一种是:
<?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
) . "\n";
}
?>
我想也许我可以调用的功能等
<?php links( array( 'affiliate', 7 ) ); ?>
要么
<?php links( array( 'social', 2 ) ); ?>
将它们合并为一个可重复使用的功能,节省处理时间/资源或将它不要紧?