Home  >  Article  >  Backend Development  >  How to remove the middle characters in php

How to remove the middle characters in php

WBOY
WBOYOriginal
2022-05-06 10:26:432095browse

In PHP, you can use the "substr_replace()" function to remove a few characters in the middle. This function is used to replace part of a string with another string. Just set the replaced part to Just leave it empty, and the syntax is "substr_replace(string, "", remove character position, remove character number)".

How to remove the middle characters in php

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

How to remove the middle characters in php

substr_replace() function replaces part of a string with another string.

If the start parameter is a negative number and length is less than or equal to start, length is 0.

This function is binary safe.

Syntax

substr_replace(string,replacement,start,length)
  • string Required. Specifies the string to check.

  • replacement Required. Specifies the string to be inserted.

  • start Required. Specifies where in the string to begin replacement. Positive number - starts at the specified position in the string, negative number - starts at the specified position from the end of the string, 0 - starts at the first character in the string

  • length available select. Specifies how many characters to replace. The default is the same as the string length. Positive number - the length of the string to be replaced, negative number - the number of characters to be replaced from the end of the string, 0 - insertion instead of replacement

Return value: Returns the replaced string . If string is an array, the array is returned.

The example is as follows:

<?php
echo substr_replace("Helloworldworld","",5,5); 
?>

Output result:

How to remove the middle characters in php

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to remove the middle characters 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