Get the App
SLTechnology News&Howtos  ›  Development  › 

How to remove the content after the specified location in php

Shulou Source: shulou.com Published: 2022-05-31 18:37:48 11月11日 Update

This article mainly introduces the relevant knowledge of "how php removes the content after a specified location". The editor shows you the operation process through an actual case. The operation method is simple and fast, and it is practical. I hope that this article "how to remove the content after a specified location in php" can help you solve the problem.

Removal method: 1, using the "substr_replace ($str,'', position value)" statement, you can replace all the characters after the specified position of the string with empty characters, and then remove all characters; 2, using the "array_splice ($arr, position value)" statement, you can remove all elements after the specified position of the array.

Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

Php implements two ways to remove the content after the specified location is removed:

Use the substr_replace () function

Use the array_splice () function

1. Substr_replace () function

Use substr_replace () to remove all characters after the specified position of the string (just set the replacement value to an empty character)

2. Array_splice () function

Using array_splice (), you can remove all elements after the specified position in the array.

Example: there is such an array:

The contents after deleting the first element of the array

Echo "content after deleting the first element of the array:"; array_splice ($arr,1); var_dump ($arr)

The contents after deleting the second element of the array

Echo "content after deleting the second element of the array:"; array_splice ($arr,2); var_dump ($arr)

The contents after deleting the third element of the array

Echo "content after deleting the third element of the array:"; array_splice ($arr,3); var_dump ($arr)

The contents after deleting the fourth element of the array

Echo "content after deleting the fourth element of the array:"; array_splice ($arr,4); var_dump ($arr)

This is the end of the content about "how php removes the content after the specified location". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Tags: Content characters arrays positions elements functions methods knowledge strings empty characters industries statements different practical practical tutorials articles more cases Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS MySQL OPPO Reno Apple Redmi