How do i remove a specific word from a string in php?

I'm extracting twitter user's profile image through JSON. For this my code is:

$x->profile_image_url

that returns the url of the profile image. The format of the url may be "..xyz_normal.jpg" or "..xyz_normal.png" or "..xyz_normal.jpeg" or "..xyz_normal.gif" etc.

Now I want to delete the "_normal" part from every url that I receive. How can I achieve this in php? I'm tired of trying it. Please help.

asked Aug 18, 2012 at 9:02

2

Php str_replace.

str_replace('_normal', '', $var)

What this does is to replace '_normal' with '' (nothing) in the variable $var. Or take a look at preg_replace if you need the power of regular expressions.

answered Aug 18, 2012 at 9:04

MatsemannMatsemann

20.2k18 gold badges57 silver badges88 bronze badges

3

The str_ireplace() function does the same job but ignoring the case

like the following


output : Hello Peter!

for more example you can see

answered Feb 2, 2020 at 8:07

How do i remove a specific word from a string in php?

Irshad BabarIrshad Babar

1,20318 silver badges26 bronze badges

The str_replace() function replaces some characters with some other characters in a string.

try something like this:

$x->str_replace("_normal","",$x)

answered Aug 18, 2012 at 9:11

Samy S.RathoreSamy S.Rathore

1,7853 gold badges25 silver badges43 bronze badges

$s = 'Posted On jan 3rd By Some Dude';


echo strstr($s, 'By', true);

This is to remove particular string from a string.

The result will be like this

 'Posted On jan 3rd'

answered Jan 3, 2018 at 7:03

How do i remove a specific word from a string in php?

chrischris

1916 bronze badges

Multi replace

$a = array('one','two','three');
$var = "one_1 two_2 three_3";
str_replace($a, '',$var);

How do i remove a specific word from a string in php?

answered Dec 25, 2019 at 3:09

How do i remove a specific word from a string in php?

string erase(subscript, count)
    {
     string place="New York";
     place erase(0,2)
  }

How do i remove a specific word from a string in php?

answered Sep 2, 2014 at 7:47

Not the answer you're looking for? Browse other questions tagged php string or ask your own question.

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”.

How can I remove part of a string in PHP?

The substr() and strpos() function is used to remove portion of string after certain character. strpos() function: This function is used to find the first occurrence position of a string inside another string. Function returns an integer value of position of first occurrence of string.

How do I replace a word in a string in PHP?

The str_replace() function replaces some characters with some other characters in a string. This function works by the following rules: If the string to be searched is an array, it returns an array. If the string to be searched is an array, find and replace is performed with every array element.

How do you remove portion of a string before a certain character in PHP?

You can use strstr to do this. Show activity on this post. The explode is in fact a better answer, as the question was about removing the text before the string.

What is TRIM function in PHP?

The trim() function removes whitespace and other predefined characters from both sides of a string. Related functions: ltrim() - Removes whitespace or other predefined characters from the left side of a string.