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

Use PHP to remove certain characters or words from a string

by Martin Jost
No comments
1 min Reading Time
6.52 K Views
4.25 / 5 Score
3 Likes
Share on Facebook Tweet on Twitter Share on Linkedin Pin on Pinterest Send via E-Mail Send via WhatsApp

The default PHP function str_replace(); is a useful tool if we want to remove certain characters, symbols or words from a string. Below I show you how to work with it.

How do I remove specific characters from a string?

The following wrapper function shows how we can use PHP to remove specific characters from a value or text. We simply pass the text or value to the function. This is done as follows: clear_string_from_characters("A [text] with $ characters");

  • Start value: A [text] with $ characters.
  • End value: A text with characters.

In the above function we can see the array(); contains characters defined that should not be included in the output. With the variable $replace = ''; We have decided that the previously defined characters are replaced by an empty value and will be completely removed. Therefore, the characters “[“, “]” and “$” are removed from the start value.

How do I remove words from a string?

The next example shows how to replace the number “6” with an empty value and the word “old” with “young”.

  • Start value: I’m 68 years old.
  • End value: I’m 8 years young.

Of course, if you do not need a wrapper function, you can just use the default PHP function str_replace(); use, as the following example shows:

In the above example code by using PHP we have replaced the word “Hello” by “Howdy”.

Last updated: December 5, 2019

Use PHP to remove certain characters or words from a string Score: 4.25 / 5 (4votes)
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
G
PHP Get the absolute File Path (Document Root-Path) in WordPress

Wenn man als Webentwickler mit Dateien arbeitet, die auf dem Server liegen und diese innerhalb e ...

Read More
A
WordPress Anonymize IP address of WordPress comments

In the context of the European privacy policy regulations some regulations for web page owners a ...

Read More
O
WordPress Open Graph in WordPress einbetten, ohne Plugin

Das Open Graph-Protokoll ermöglicht es, dass jede Webseite als “rich object” im &#82 ...

Read More
Read more
G
Previous Post
Get the current WordPress URL
G
Next Post
Get Metadata from WordPress Video and Audio files
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