How do i remove a 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

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

chrischris

1916 bronze badges

Multi replace

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

answered Dec 25, 2019 at 3:09

string erase[subscript, count]
    {
     string place="New York";
     place erase[0,2]
  }

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.

❮ PHP String Reference

Example

Remove characters from the right end of a string:

Try it Yourself »

Definition and Usage

The chop[] function removes whitespaces or other predefined characters from the right end of a string.

Syntax

Parameter Values

ParameterDescription
string Required. Specifies the string to check
charlist Optional. Specifies which characters to remove from the string.
The following characters are removed if the charlist parameter is empty:
  • "\0" - NULL
  • "\t" - tab
  • "\n" - new line
  • "\x0B" - vertical tab
  • "\r" - carriage return
  • " " - ordinary white space

Technical Details

Return Value:PHP Version:Changelog:
Returns the modified string
4+
The charlist parameter was added in PHP 4.1.0

More Examples

Example

Remove newlines [\n] from the right end of a string:

The HTML output of the code above will be [View Source]:


Hello World!

Hello World!


The browser output of the code above will be:

Hello World! Hello World!

Try it Yourself »

❮ PHP String Reference


How do I remove a specific word from a string in PHP?

Definition and Usage 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 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.

What does the chop function do in PHP?

The chop[] function removes whitespaces or other predefined characters from the right end of a string.

How can I replace multiple words in a string in PHP?

Approach 1: Using the str_replace[] and str_split[] functions in PHP. The str_replace[] function is used to replace multiple characters in a string and it takes in three parameters. The first parameter is the array of characters to replace.

Chủ Đề