Home  >  Article  >  Backend Development  >  How to remove quotes before and after a string in php

How to remove quotes before and after a string in php

王林
王林Original
2020-07-13 15:21:184439browse

The way PHP removes the quotes before and after a string is: you can first use the preg_match() function to determine whether the string starts and ends with quotes, and then use the substr() function and strlen() function to extract the quotes. The string can be.

How to remove quotes before and after a string in php

To remove the quotes before and after the string, you can use the preg_match() function to determine whether the string starts and ends with quotes, and then use the substr() function with The strlen() function gets the string content in quotes and puts it into a new array.

(Recommended learning: php tutorial)

Related function introduction:

substr() function returns part of the string.

Syntax:

substr(string,start,length)

Return value:

Returns the extracted part of the string, returns FALSE if it fails, or returns an empty string.

Code implementation:

function remove_quote(&$str) {
        if (preg_match("/^\"/",$str)){
            $str = substr($str, 1, strlen($str) - 1);
        }
        //判断字符串是否以'"'结束
        if (preg_match("/\"$/",$str)){
            $str = substr($str, 0, strlen($str) - 1);;
        }
        return $str;
  }

The above is the detailed content of How to remove quotes before and after a string in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn