php 从字符串中删除特定单词

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12017323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:39:02  来源:igfitidea点击:

Delete particular word from string

phpstring

提问by user188995

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

我正在通过 JSON 提取 Twitter 用户的个人资料图片。为此,我的代码是:

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

返回个人资料图片的网址。url 的格式可以是"..xyz_normal.jpg"or"..xyz_normal.png""..xyz_normal.jpeg"or"..xyz_normal.gif"等。

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.

现在我想从我收到的每个 url 中删除“_normal”部分。我怎样才能在 php 中实现这一点?我厌倦了尝试。请帮忙。

回答by Matsemann

Php str_replace.

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_replaceif you need the power of regular expressions.

这样做是将变量 $var 中的 '_normal' 替换为 ''(无)。或者,如果您需要正则表达式的强大功能,请查看preg_replace

回答by Samy S.Rathore

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

str_replace() 函数用字符串中的一些其他字符替换一些字符。

try something like this:

尝试这样的事情:

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

回答by chris

$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'

回答by HEA

Multi replace

多重替换

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

回答by I B

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

str_ireplace() 函数执行相同的工作,但忽略大小写

like the following

像下面这样

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

output : Hello Peter!

输出:你好彼得!

for more example you can see

有关更多示例,您可以看到

回答by ann

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