Regular expressions can be used to match arbitrary dates and replace them with an empty string; the code is as follows:
$_POST['newstext'] = preg_replace('/\/file\/\d{8}\//', '/file/', $_POST['newstext']);Regular expressions are used here./\/file\/\d{8}\//Match something similar to/file/20230101/Such a string, where\d{8}Represents an 8-bit numeric value. Then use it.preg_replace()The function replaces the matched string with/file/Note: The slash in a regular expression must be escaped.
If you are not familiar with regular expressions, you can refer to online regular expression tutorials and tools, such as RegexOne 和 RegExr。
The date is not fixed; sometimes it represents a string of unknown length.
If the date-part string has an unknown length, you can use a regular expression to match a substring within it; the code is as follows:
$_POST['newstext'] = preg_replace('/\/file\/\w+\//', '/file/', $_POST['newstext']);The regular expression here/\w+/Matches one or more letters, digits, or underscore characters, representing the date portion. Then usepreg_replace()The function replaces the matched string with/file/Similarly, the slashes before and after the matched string should also be included.
Note: This method may replace strings that do not contain date components, for example./file/abc123/Therefore, it is necessary to determine whether to use this replacement method based on the specific circumstances.