php - convert single quoted string to double quoted

前端 未结 3 1770
谎友^
谎友^ 2021-01-20 07:30

Been searching here and google for over an hour, can\'t seem to find the answer to this.

I have a string returned from a database query which contains variables, how

3条回答
  •  抹茶落季
    2021-01-20 08:24

    PHP does not have a standard safe way to do this, right now. There has been an open feature request for years asking for it: http://bugs.php.net/bug.php?id=43901

    One of the comments on the ticket offers a regex to do simple $foo and ${foo} substitution:

    function stringExpand($subject, array $vars) {
      foreach ($vars as $name => $value) {
        $subject = preg_replace(sprintf('/\$\{?%s\}?/', $name), $value, $subject);
      }
      return $subject;
    }
    

提交回复
热议问题