Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将单词替换为星形(精确长度)_Php_Replace_Str Replace - Fatal编程技术网

Php 将单词替换为星形(精确长度)

Php 将单词替换为星形(精确长度),php,replace,str-replace,Php,Replace,Str Replace,我正在尝试替换字符串中的一个单词,但我想获取函数中找到的单词,并将其替换为具有精确长度的星号的单词 这是可能的还是我需要用其他方式来做 $text = "Hello world, its 2018"; $words = ['world', 'its']; echo str_replace($words, str_repeat("*", count(FOUND) ), $text); 您可以使用正则表达式来执行此操作: $text = preg_replace_callback('~(?:'

我正在尝试替换字符串中的一个单词,但我想获取函数中找到的单词,并将其替换为具有精确长度的星号的单词

这是可能的还是我需要用其他方式来做

$text = "Hello world, its 2018";
$words = ['world', 'its'];


echo str_replace($words, str_repeat("*", count(FOUND) ), $text);

您可以使用正则表达式来执行此操作:

$text = preg_replace_callback('~(?:'.implode('|',$words).')~i', function($matches){
    return str_repeat('*', strlen($matches[0]));
}, $text);
echo $text ; // "Hello *****, *** 2018"
在使用
preg\u replace\u callback()
之前,您还可以使用
preg\u quote
来确保此安全:


编辑:以下代码是另一种方式,使用
foreach()
循环,但防止不必要的行为(替换部分单词),并允许使用多字节字符:

$words = ['foo', 'bar', 'bôz', 'notfound'];
$text = "Bar&foo; bAr notfoo, bôzo bôz :Bar! (foo), notFOO and NotBar or 'bar' foo";
$expt = "***&***; *** notfoo, bôzo *** :***! (***), notFOO and NotBar or '***' ***";

foreach ($words as $word) {
    $text = preg_replace_callback("~\b$word\b~i", function($matches) use ($word) {
        return str_ireplace($word, str_repeat('*', mb_strlen($word)), $matches[0]);
    }, $text);
}

echo $text, PHP_EOL, $expt ;

您可以使用正则表达式来执行此操作:

$text = preg_replace_callback('~(?:'.implode('|',$words).')~i', function($matches){
    return str_repeat('*', strlen($matches[0]));
}, $text);
echo $text ; // "Hello *****, *** 2018"
在使用
preg\u replace\u callback()
之前,您还可以使用
preg\u quote
来确保此安全:


编辑:以下代码是另一种方式,使用
foreach()
循环,但防止不必要的行为(替换部分单词),并允许使用多字节字符:

$words = ['foo', 'bar', 'bôz', 'notfound'];
$text = "Bar&foo; bAr notfoo, bôzo bôz :Bar! (foo), notFOO and NotBar or 'bar' foo";
$expt = "***&***; *** notfoo, bôzo *** :***! (***), notFOO and NotBar or '***' ***";

foreach ($words as $word) {
    $text = preg_replace_callback("~\b$word\b~i", function($matches) use ($word) {
        return str_ireplace($word, str_repeat('*', mb_strlen($word)), $matches[0]);
    }, $text);
}

echo $text, PHP_EOL, $expt ;
您可以尝试以下方法:

$text = "Hello world, its 2018";
$words = ['world', 'its'];

// Loop through your word array
foreach ($words as $word) {
    $length = strlen($word);                    // length of the word you want to replace
    $star   = str_repeat("*", $length);         // I build the new string ****
    $text   = str_replace($word, $star, $text); // I replace the $word by the new string
}

echo $text; // Hello *****, *** 2018
这就是你要找的吗?

你可以试试这个:

$text = "Hello world, its 2018";
$words = ['world', 'its'];

// Loop through your word array
foreach ($words as $word) {
    $length = strlen($word);                    // length of the word you want to replace
    $star   = str_repeat("*", $length);         // I build the new string ****
    $text   = str_replace($word, $star, $text); // I replace the $word by the new string
}

echo $text; // Hello *****, *** 2018
这就是你想要的吗?

另一种方法:

$text = "Hello world, its 2018";
$words = ['world', 'its'];

$f = function($value) { return str_repeat("*", strlen($value)) ; } ;
$replacement = array_map($f, $words);
echo str_replace($words, $replacement, $text);
另一种方法:

$text = "Hello world, its 2018";
$words = ['world', 'its'];

$f = function($value) { return str_repeat("*", strlen($value)) ; } ;
$replacement = array_map($f, $words);
echo str_replace($words, $replacement, $text);

你可以这样走

$text = "Hello crazy world, its 2018";
$words = ['world', 'its'];

array_walk($words,"replace_me");

function replace_me($value,$key)
{
    global $text;
    $text = str_replace($value,str_repeat("*",strlen($value)),$text);
}

echo $text;

你可以这样走

$text = "Hello crazy world, its 2018";
$words = ['world', 'its'];

array_walk($words,"replace_me");

function replace_me($value,$key)
{
    global $text;
    $text = str_replace($value,str_repeat("*",strlen($value)),$text);
}

echo $text;


为了确定,您想将
世界
(5个字母)替换为
******
(5颗星),并将
替换为
***
(3颗星)?@Zoric您是否使用此技术隐藏亵渎文字?或者让用户名匿名?在这项任务中有更多的注意事项需要实施。当
$text=“科学:可能永远无法回答的问题;宗教:永远无法回答的问题”时,您的预期结果是什么
$words=[“问题”,“答案]?@mickmackusa我用这个来审查评论中的单词请表达我的示例输入的预期结果。你将如何处理像“暗杀”这样的词?如果您乐于忽略单词边界,则不应使用正则表达式。@mickmackusa我已经从Syscall获得了一个工作代码,我对其进行了修改,以确保您希望将
world
(5个字母)替换为
******
(5颗星),并将
its
替换为
***
(3颗星)?@Zoric你是在用这种技巧隐藏亵渎的文字吗?或者让用户名匿名?在这项任务中有更多的注意事项需要实施。当
$text=“科学:可能永远无法回答的问题;宗教:永远无法回答的问题”时,您的预期结果是什么
$words=[“问题”,“答案]?@mickmackusa我用这个来审查评论中的单词请表达我的示例输入的预期结果。你将如何处理像“暗杀”这样的词?如果您乐于忽略单词边界,则不应使用regex。@mickmackusa我已经从Syscall获得了一个工作代码,我想与您分享我的见解。考虑这个假设的字符串:<代码>渔夫的妻子,Cassy,假设她的丈夫会暗杀一个满是低音的桶。< /代码>我们将寻求替换<代码> ASS<代码>。因为您没有缓存
ass
***
)的替换字符串,所以您的方法在每次匹配的事件上都调用
strlen()
stru replace()
。缓存替换项的其他答案仅为每针调用一次这对函数。本页上的所有答案都应使用不区分大小写,以最大限度地包含在内。这项任务不需要正则表达式,也就是说,如果你有一个12000个单词的坏单词词典,那么预先确定替换字符串的代价会非常昂贵。我认为最合适的方法可能是花时间构建一个臃肿的字典数组,用坏单词作为键,用硬编码的重复星号作为值。这意味着节省了处理时间和内存成本。我应该通过“预先确定”来澄清我的意思:迭代所有坏字并生成/存储它们特定长度的替换字符串。@mickmackusa绝对正确。非常感谢您提出这些明智的意见。您建议如何改进此堆栈溢出页面?我必须通过添加更有效的答案来编辑我的帖子吗?@mickmackusa谢谢你的编辑。也就是说,Stack Overflow:“接受并不意味着它是最好的答案,它只意味着它对提问的人有效。”我想与你分享我的见解。考虑这个假设的字符串:<代码>渔夫的妻子,Cassy,假设她的丈夫会暗杀一个满是低音的桶。< /代码>我们将寻求替换<代码> ASS<代码>。因为您没有缓存
ass
***
)的替换字符串,所以您的方法在每次匹配的事件上都调用
strlen()
stru replace()
。缓存替换项的其他答案仅为每针调用一次这对函数。本页上的所有答案都应使用不区分大小写,以最大限度地包含在内。这项任务不需要正则表达式,也就是说,如果你有一个12000个单词的坏单词词典,那么预先确定替换字符串的代价会非常昂贵。我认为最合适的方法可能是花时间构建一个臃肿的字典数组,用坏单词作为键,用硬编码的重复星号作为值。这意味着节省了处理时间和内存成本。我应该通过“预先确定”来澄清我的意思:迭代所有坏字并生成/存储它们特定长度的替换字符串。@mickmackusa绝对正确。非常感谢您提出这些明智的意见。您建议如何改进此堆栈溢出页面?我必须通过添加更有效的答案来编辑我的帖子吗?@mickmackusa谢谢你的编辑。也就是说,Stack Overflow:“接受并不意味着它是最好的答案,它只意味着它对提出问题的人有效。”