Home  >  Article  >  Backend Development  >  How to replace single quotes in string in php

How to replace single quotes in string in php

WBOY
WBOYOriginal
2022-04-13 11:12:422381browse

In PHP, you can use the "str_replace()" function to replace single quotes in a string. The function of this function is to replace some characters in the string and is case-sensitive. The syntax is "str_replace(" '","replaced content",string);".

How to replace single quotes in string in php

The operating environment of this article: Windows 10 system, PHP version 7.1, Dell G3 computer

How to replace single quotes in strings in php

str_replace() function replaces some characters in a string (case sensitive).

This function must follow the following rules:

  • If the searched string is an array, then it will return an array.

  • If the searched string is an array, then it will find and replace each element in the array.

  • If an array needs to be searched and replaced at the same time, and the elements to be replaced are less than the number of found elements, the excess elements will be replaced with empty strings .

  • If you search an array and replace only one string, the replacement string will work for all found values.

The syntax is

str_replace(find,replace,string,count)
  • find required. Specifies the value to look for.

  • replace Required. Specifies the value to replace the value in find.

  • string Required. Specifies the string to be searched for.

  • count Optional. A variable counting the number of substitutions.

The example is as follows:

<?php
$a="这原本是一个单引号";
$b="括号里面是一个单引号(&#39;)后来被替换了";
echo str_replace("&#39;",$a,$b);
?>

Output result:

How to replace single quotes in string in php

Recommended learning: "PHP video tutorial

The above is the detailed content of How to replace single quotes in 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