drvxnivoqf17568697 2012-05-07 18:48
浏览 87
已采纳

将动态参数传递给函数

I have a user defined function below:

function char_replace($line1){
    $line1= str_ireplace("Snippet:", "", $line1);
    // First, replace UTF-8 characters.
    $line1= str_replace(
    array("\xe2\x80\x98", "\xe2\x80\x99", "\xe2\x80\x9c", "\xe2\x80\x9d", "\xe2\x80\x93", "\xe2\x80\x94", "\xe2\x80\xa6"),
    array("'", "'", '"', '"', '-', '--', '...'),
    $line1);
    // Next, replace their Windows-1252 equivalents.
    $line1= str_replace(
    array(chr(145), chr(146), chr(147), chr(148), chr(150), chr(151), chr(133)),
    array("'", "'", '"', '"', '-', '--', '...'),
    $line1);
}

and i'm replacing characters on multiple lines that i've exploded, except i want to apply a dynamic argument to the function char_replace where $line could very well be $line2 or $line3 so i would convert the characters this way: $line1 = char_replace($line1)

I want to make the function arguments and the str_replace/str_ireplace arguments to be a dynamic variable, where i could just convert another line like so: $random_line = char_replace($random_line) Is this possible?

  • 写回答

3条回答 默认 最新

  • dongleiqiao4906 2012-05-07 18:56
    关注

    Assuming that you end your function with return $line1; you can call it like this:

    $line1 = char_replace($line1);
    $line2 = char_replace($line2);
    $line3 = char_replace($line3);
    

    How you call the arguments in your function definition doesn't matter, they're local to that function and can have a different name outside of it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题