How to remove underline in php

藏色散人
Release: 2023-03-10 13:36:02
Original
2699 people have browsed it

php method to remove underlines: first create a PHP sample file; then remove the underlines through "preg_replace( '/_[^_]*$/', '', your_string );".

How to remove underline in php

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

Specific questions:

How to remove underline in php?

php - How to remove the last occurrence of underscore in a string

I have a string that contains many underscores followed by words for example: "Field_4_txtbox" I need to find the last one in the string underscore and remove everything after it (including the "_") so it returns me "Field_4" but I need it to handle different lengths of trailing strings. So I can't just trim a fixed length.

I know I can do an If statement to check for certain endings like

if(strstr($key,'chkbox')) { $string= rtrim($key, '_chkbox'); }
Copy after login

but I want to do this in one go using a regex pattern, how can I do it?

Solution:

The matching regular expression will be:

/_[^_]*$/
Copy after login

Just replace it with ":

preg_replace( '/_[^_]*$/', '', your_string );
Copy after login

[Recommended learning:PHP video tutorial]

The above is the detailed content of How to remove underline in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!