drvxnivoqf17568697
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
    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
    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
    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;
    }
    
    点赞 评论

相关推荐