user188995
user188995

Reputation: 557

Delete particular word from string

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.

Upvotes: 28

Views: 124095

Answers (5)

chris
chris

Reputation: 191

$s = 'Posted On jan 3rd By Some Dude';
var_export(strstr($s, 'By', true));

This return the leading portion of the string before the needle value.

The result will be: https://3v4l.org/437SX

'Posted On jan 3rd '

Upvotes: 4

Irshad Babar
Irshad Babar

Reputation: 1419

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

like the following

<?php
echo str_ireplace("World","Peter","Hello world!");
?>

output : Hello Peter!

for more example you can see

Upvotes: 6

HEA
HEA

Reputation: 11

Multi replace

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

Upvotes: 1

Matsemann
Matsemann

Reputation: 21844

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.

Upvotes: 95

Samy S.Rathore
Samy S.Rathore

Reputation: 1823

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

try something like this:

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

Upvotes: 4

Related Questions