【问题标题】:how to extract value from variable in PHP function?如何从 PHP 函数中的变量中提取值?
【发布时间】:2011-06-10 06:55:21
【问题描述】:

这可能是一个愚蠢的问题,但是如何在 PHP 函数中提取变量的值?我在stackoverflow上找到了如何找到网页标题的代码:

function page_title($url) 
      {
        $fp = file_get_contents($url);
        if (!$fp) 
            return null;

        $res = preg_match("/<title>(.*)<\/title>/", $fp, $title_matches);
        if (!$res) 
            return null; 

        $title = $title_matches[1];
        return $title;
      }

我在上面的函数之外有一个名为 $extract 的变量,我想将函数中的 $title 的值插入到外部变量 $extract 中。我假设您必须先调用该函数,然后执行其他操作以实现此目的,但我不知道该步骤是什么。

如果我调用函数并且变量 $title 返回值“欢迎来到我的网站”,我想将该值与外部变量 $extract 相关联。

【问题讨论】:

    标签: php function variables extract


    【解决方案1】:
    $extract = page_title($url);
    

    把它放在函数之外,你应该很高兴

    【讨论】:

      【解决方案2】:

      您可以简单地将函数返回的值分配给您的属性:

      $value = somefunc();
      echo $value;
      
      function somefunc()
      {
          return "trendy value";
      }
      

      【讨论】:

        【解决方案3】:

        随便写

        $extract = page_title($url); 
        

        在你的职能之外。即像上面的行一样进行函数调用,返回的值是 null 还是 $title 将被存储以提取

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-12-05
          相关资源
          最近更新 更多