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?

图片转代码服务由CSDN问答提供 功能建议

我在下面有一个用户定义的函数:

  function char_replace  ($ line1){
 $ line1 = str_ireplace(“Snippet:”,“”,$ line1); 
 //首先,替换UTF-8字符。
 $ 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); 
 //接下来,替换他们的Windows-1252当量。
 $ line1 = str_replace(
 array(chr(145),chr(146),chr(147),chr(148),chr(150)  ),chr(151),chr(133)),
 array(“'”,“'”,“”,“”,“ - ”,“ - ”,“......”),\  n $ line1); 
} 
   
 
 

我正在替换我爆炸的多行上的字符,除了我想将动态参数应用于 函数char_replace其中 $ line 很可能是 $ line2 $ line3 所以我会这样转换字符: $ line1 = char_replace($ line1)

我想让函数参数和str_replace / str_ireplace参数成为一个动态变量,我可以像这样转换另一行: $ random_line = char_replace($ random_line) 这可能吗?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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.

    打赏 评论
  • duanba4254 2012-05-07 18:57

    If I'm reading this right, just add a return to the function. So:

    function char_replace($string){
      $string= str_ireplace("Snippet:", "", $string);
      // First, replace UTF-8 characters.
      $string= 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("'", "'", '"', '"', '-', '--', '...'),
      $string);
      // Next, replace their Windows-1252 equivalents.
      $string= str_replace(
      array(chr(145), chr(146), chr(147), chr(148), chr(150), chr(151), chr(133)),
      array("'", "'", '"', '"', '-', '--', '...'),
      $string);
    
      return $string;
    }
    

    This will allow you to pass any string to the function and get the modified string back.

    打赏 评论
  • dongshi4589 2012-05-07 18:57

    Do you just want to add return statement to your function:

    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);
        return $line1;
    }
    
    打赏 评论

相关推荐 更多相似问题