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
4.19 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
C
PHP Customize the default WordPress Image Compression

WordPress uses its own compression for images that are uploaded directly into WordPress. This ha ...

Read More
C
WordPress Customize or Modify the WordPress Post Excerpt

WordPress comes with own functions, such as get_the_excerpt() or the_excerpt(), to output a shor ...

Read More
G
PHP Get the current WordPress URL

In some cases, it may be necessary to get the current WordPress URL of where the user is current ...

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-2022 - 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