php 多个项目的 Str_replace

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7605480/
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-26 03:01:23  来源:igfitidea点击:

Str_replace for multiple items

phpstringstr-replace

提问by sameold

I remember doing this before, but can't find the code. I use str_replace to replace one character like this: str_replace(':', ' ', $string);but I want to replace all the following characters \/:*?"<>|, without doing a str_replace for each.

我记得以前这样做过,但找不到代码。我使用 str_replace 来替换这样的一个字符:str_replace(':', ' ', $string);但我想替换以下所有字符\/:*?"<>|,而不对每个字符进行 str_replace 。

回答by Dogbert

Like this:

像这样:

str_replace(array(':', '\', '/', '*'), ' ', $string);

Or, in modern PHP (anything from 5.4 onwards), the slighty less wordy:

或者,在现代 PHP(从 5.4 开始的任何版本)中,稍微不那么冗长:

str_replace([':', '\', '/', '*'], ' ', $string);

回答by NullUserException

str_replace()can take an array, so you could do:

str_replace()可以接受一个数组,所以你可以这样做:

$new_str = str_replace(str_split('\/:*?"<>|'), ' ', $string);

Alternatively you could use preg_replace():

或者,您可以使用preg_replace()

$new_str = preg_replace('~[\\/:*?"<>|]~', ' ', $string);

回答by Sumoanand

For example, if you want to replace search1 with replace1 and search2 with replace2 then following code will work:

例如,如果你想用 replace1 替换 search1,用 replace2 替换 search2,那么下面的代码将起作用:

print str_replace(
    array("search1","search2"),
    array("replace1", "replace2"),
    "search1 search2"
);

// Output: replace1 replace2

// 输出:replace1replace2

回答by Marty

str_replace(
    array("search","items"),
    array("replace", "items"),
    $string
);

回答by Explosion Pills

If you're only replacing single characters, you should use strtr()

如果你只替换单个字符,你应该使用 strtr()

回答by GreenMatt

You could use preg_replace(). The following example can be run using command line php:

您可以使用preg_replace()。可以使用命令行 php 运行以下示例:

<?php
$s1 = "the string \/:*?\"<>|";
$s2 = preg_replace("^[\\/:\*\?\"<>\|]^", " ", $s1) ;
echo "\n$s2: \"" . $s2 . "\"\n";
?>

Output:

输出:

$s2: "the string          "

$s2: "字符串"

回答by Tempteh

I had a situation whereby I had to replace the HTML tags with two different replacement results.

我遇到了一种情况,我不得不用两种不同的替换结果来替换 HTML 标签。

$trades = "<li>Sprinkler and Fire      Protection Installer</li>
<li>Steamfitter </li>
<li>Terrazzo, Tile and Marble      Setter</li>";

$s1 =  str_replace('<li>', '"', $trades);

$s2 = str_replace('</li>', '",', $s1);

echo $s2;

result

结果

"Sprinkler and Fire Protection Installer", "Steamfitter ", "Terrazzo, Tile and Marble Setter",

“洒水喷头和防火安装工”、“Steamfitter”、“水磨石、瓷砖和大理石安装工”、

回答by Erald Karakashi

I guess you are looking after this:

我猜你正在照顾这个:

// example
private const TEMPLATE = __DIR__.'/Resources/{type}_{language}.json';

...

public function templateFor(string $type, string $language): string
{
   return \str_replace(['{type}', '{language}'], [$type, $language], self::TEMPLATE);
}

回答by dataviews

In my use case, I parameterized some fields in an HTML document, and once I load these fields I match and replace them using the str_replace method.

在我的用例中,我参数化了 HTML 文档中的一些字段,一旦我加载了这些字段,我就会使用 str_replace 方法匹配和替换它们。

<?php echo str_replace(array("{{client_name}}", "{{client_testing}}"), array('client_company_name', 'test'), 'html_document'); ?>