在同一字符串上多次使用str_replace [英] Using str_replace multiple times on the same string
问题描述
我正在遍历表格中的标题,因此本质上是沿着这些行的内容.
I'm looping through a title from a table so it's essentially something along these lines.
foreach($c as $row){
echo string_shorten($row['title']);
}
我正在尝试的是一条switch语句,该语句将在我要搜索的内容和找到的内容之间进行切换,将其替换为我在str_replace中选择的内容:
What I'm doing is trying is a switch statement that would switch between what I want it to search for and once it's found replace it with what I choose in the str_replace:
function string_shorten($text){
switch(strpos($text, $pos) !== false){
case "Hi":
return str_replace('Hi','Hello', $text);
break;
}
}
任何建议或可能的替代方案将不胜感激.感觉我真的很接近,但还不是很接近.
Any suggestions or possible alternatives would be appreciated. It feels like I'm really close but not quite.
推荐答案
混合
str_replace
(混合$search
,混合$replace
,混合$subject
[,int&$count
])
mixed
str_replace
( mixed$search
, mixed$replace
, mixed$subject
[, int&$count
] )
以及本例
// Provides: You should eat pizza, beer, and ice cream every day
$phrase = "You should eat fruits, vegetables, and fiber every day.";
$healthy = array("fruits", "vegetables", "fiber");
$yummy = array("pizza", "beer", "ice cream");
$newphrase = str_replace($healthy, $yummy, $phrase);
这意味着您可以使用类似以下的内容
This means that you could use something like the following
$search = array('Hi', 'Heyo', 'etc.');
$replace = array('Hello', 'Hello', '');
$str = str_replace($search, $replace, $str);
这篇关于在同一字符串上多次使用str_replace的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!