The Must Have for WordPress
The Must Have for WordPress
×
  • Home
December 5, 2019
PHP

Trim a PHP string before the last special character

by Martin Jost
No comments
1 min Reading Time
7.11 K Views
5 Score
1 Like
Share on Facebook Tweet on Twitter Share on Linkedin Pin on Pinterest Send via E-Mail Send via WhatsApp

This small PHP function is helping us to get only a part of a value (string) by using a specific character as an indicator.

How do I get only the last part of a value?

We can use the following function to get only a value after the last character we’ve passed to the function. For example, $string = "123456"; is the start value and $character = "5"; the last character we use to get the part, after exactly that character.

An example:

In this example, the start value is a URL and in result we only want to get the attached parameter value.

  • Start value: https://domain.com/?color=black
  • End Value: color=black

An additional example:

In this example, we have a text string as start value and we only want to get the gender.

  • Start value: I’m male
  • End Value: male

Last updated: December 5, 2019

Trim a PHP string before the last special character Score: 5 / 5 (1vote)
Share on Facebook Tweet on Twitter Share on Linkedin Pin on Pinterest Send via E-Mail Send via WhatsApp
The Author
Published by
Martin Jost

View all posts by Martin Jost

Related Posts
M
Javascript Minify and compress Javascript files in PHP

Loading time is an important factor in search engine optimization and for the user experience. E ...

Read More
G
PHP Get Metadata from WordPress Video and Audio files

In addition to images, videos and audio files are other formats that we can upload to WordPress ...

Read More
A
WordPress Add WordPress images to the RSS feed

By default, if you have enabled the WordPress RSS feed, images will not be included in the RSS f ...

Read More
Read more
G
Previous Post
Get the absolute File Path (Document Root-Path) in WordPress
C
Next Post
Compress files with Gzip in PHP
Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Imprint
  • Privacy Policy
© 2016-2023 - wphave - The Must Have for WordPress
Login

Lost your password?

RequiredEssential cookies enable basic functions and are necessary for the proper functioning of the website.
  • Name: Cookie Status
  • Provider: Owner of this website
  • Purpose: Is necessary to check whether the user's web browser allows cookies.
  • Privacy Policy: https://wphave.com/privacy-policy/
  • Cookie Name(s): cookie_status
  • Duration: 1 Minute
  • Name: Cookie Consent
  • Provider: Owner of this website
  • Purpose: Is necessary to check whether the user agrees to allow opt-in cookies or not.
  • Privacy Policy: https://wphave.com/privacy-policy/
  • Cookie Name(s): cookieconsent_analytics, cookieconsent_marketing, cookieconsent_status
  • Duration: 1 Year
  • Name: Like Button
  • Provider: Owner of this website
  • Purpose: Is necessary to check whether the user has already gave a like for a specific post.
  • Privacy Policy: https://wphave.com/privacy-policy/
  • Cookie Name(s): like_btn_{POST_ID}
  • Duration: 21 Days
  • Name: Post Rating
  • Provider: Owner of this website
  • Purpose: Is necessary to check whether the user has already rate a specific post.
  • Privacy Policy: https://wphave.com/privacy-policy/
  • Cookie Name(s): rating_{POST_ID}
  • Duration: 7 Days
AnalyticsStatistics cookies collect information anonymously. This information helps us to understand how our visitors use our website.
  • Name: Google Analytics
  • Provider: Google LLC
  • Purpose: Google cookie for website analysis. Generates statistical data on how the visitor uses the website.
  • Privacy Policy: https://policies.google.com/privacy
  • Cookie Name(s): _ga, _gat, _gid
  • Duration: 2 Years