【问题标题】:Find words of specific length in a string php在字符串php中查找特定长度的单词
【发布时间】:2018-03-27 11:36:32
【问题描述】:

我需要在带有length >= 6 的字符串中查找单词,并将第6 个字符之后的任何字符替换为*

似乎很困惑如何遍历 $parts 数组来查找这 ​​6 个字母的单词。

有人可以帮忙吗?

$a = 'The game is set in "The City", a dark fantasy world inspired by 
Victorian, Gothic, and steampunk aesthetics. Players control Garrett, a 
master thief who embarks on several missions focusing on stealing from the 
rich. Players may approach levels in a variety of different ways; players 
can choose the action oriented and lethal approach, where players will 
disable or kill enemies on their way to their destination, using knives and 
takedowns, or opt for the non-lethal stealthy approach, where players 
minimize interaction with NPCs and the environment in order to avoid 
detection. Players also may choose which path to take to their destination, 
as each location contains several branching paths.';

$length = 6;

$parts = explode(' ',$a);

然后

for ($i = 0; $i == $length; $i++){
    echo str_replace($parts, '*', $a);
}

【问题讨论】:

  • 这听起来像是功课
  • 你在explode.pass $a中传递$x而不是$a到explode函数中。
  • @Awaisfiaz 哦,是的。谢谢。完成
  • @NigelRen 很明显。一段时间以来,我一直在尝试自己提出解决方案。出于沮丧而询问并处于死胡同。有什么问题吗?
  • 很难获得答案可能远远超出您所学的范围,因此很明显这不是您自己的工作。无论如何,祝你的课程好运:)

标签: php arrays string loops


【解决方案1】:

您可以使用带有preg_replace_callback() 的正则表达式来转换长度为6 个或更多字符的单词。在回调内部,使用 substr() 替换以获得前 6 个字符,并根据需要附加尽可能多的 * 以获得单词的大小。

$a = 'The game is set in "The City", a dark fantasy world inspired by
Victorian, Gothic, and steampunk aesthetics. Players control Garrett, a
master thief who embarks on several missions focusing on stealing from the
rich. Players may approach levels in a variety of different ways; players
can choose the action oriented and lethal approach, where players will
disable or kill enemies on their way to their destination, using knives and
takedowns, or opt for the non-lethal stealthy approach, where players
minimize interaction with NPCs and the environment in order to avoid
detection. Players also may choose which path to take to their destination,
as each location contains several branching paths.';

$a = preg_replace_callback('~\b\w{6,}\b~', function($matches) {
  // return the 6 first characters as clean, and the rest with *
  return substr($matches[0], 0, 6) . str_repeat('*', strlen($matches[0]) - 6);
}, $a);
echo $a ;

输出:

The game is set in "The City", a dark fantas* world inspir** by
Victor***, Gothic, and steamp*** aesthe****. Player* contro* Garret*, a
master thief who embark* on severa* missio** focusi** on steali** from the
rich. Player* may approa** levels in a variet* of differ*** ways; player*
can choose the action orient** and lethal approa**, where player* will
disabl* or kill enemie* on their way to their destin*****, using knives and
takedo***, or opt for the non-lethal stealt** approa**, where player*
minimi** intera***** with NPCs and the enviro***** in order to avoid
detect***. Player* also may choose which path to take to their destin*****,
as each locati** contai** severa* branch*** paths.

正则表达式:

\b     # wordboundary
\w{6,} # letter of 6 or more length
\b     # wordboundary

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-21
    • 2013-11-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-31
    • 2016-05-28
    • 2015-04-23
    相关资源
    最近更新 更多