我无法将它包裹在我的脑海中,如何将字符串的字符串与字符串或数组中的元素进行比较。
我正在寻找的结果是这样的,我想检查概念这个词 c 。
同时仍然显示概念这个词,并且对于 c 的第一次出现,将其加粗或加下划线只是为了使其突出。
我觉得我走在正确的轨道上,需要一点指导。
<?php
$word = "conrad";
$str = array("Concept", "Support", "Design", "Fresh", "Aged","Glad");
echo $test = implode(" ", $str). '<br />';
foreach(str_split($word) as $position => $letter) {
if(stripos($str[$position], $letter) !== false) {
echo $letter .' found in <b>'. $str[$position] . '</b>';
$pos = stripos($str[$position], $letter);
echo ' posistion '. $pos .'<br>';
}
}
var_dump(str_split($word), $position, $letter, $str[$position], $test);
?>
如果有什么不清楚的地方请让我知道并且生病并尝试更好地澄清事情。
谢谢!
答案 0 :(得分:2)
我只修改了几行。我使用了substr函数来获取所选字符之前的部分,然后插入你的字符(用粗体标记包装),然后我回显出字符串的其余部分。
<?php
$word = "conrad";
$str = array(
"Concept",
"Support",
"Design",
"Fresh",
"Aged",
"Glad"
);
$string = "conrad";
echo $test = implode(" ", $str) . '<br />';
foreach(str_split($word) as $position => $letter)
{
if (stripos($str[$position], $letter) !== false)
{
$pos = stripos($str[$position], $letter);
$currentString = $str[$position];
echo $letter . ' found in ' . substr($currentString, 0, $pos) . "<b>" . $letter . "</b>" . substr($currentString, $pos + 1) . "<br />";
echo ' posistion ' . $pos . '<br />';
}
}
var_dump(str_split($word) , $position, $letter, $str[$position], $test);
?>
答案 1 :(得分:2)
这是使用正则表达式的解决方案:
$word = "conrad";
$strs = array("Concept", "Support", "Design", "Fresh", "Aged", "Glad");
foreach($strs as $i=>$str) {
echo preg_replace('/' . $word{$i} . '/i', '<b>${0}</b>', $str, 1) . "<br>\n";
}
打印出来(带有一些样式):
b { color: red; }
&#13;
<b>C</b>oncept<br>
Supp<b>o</b>rt<br>
Desig<b>n</b><br>
F<b>r</b>esh<br>
<b>A</b>ged<br>
Gla<b>d</b><br>
&#13;
答案 2 :(得分:2)
<?php
$word = "conrad";
$str = array("Concept", "Support", "Design", "Fresh", "Aged","Glad");
$test = implode(" ", $str)";
echo "{$test}<br />;
foreach(str_split($word) as $position => $letter) {
if(stripos($str[$position], $letter) !== false) {
$message = "{$letter} found in ";
// This is very basic, but gets the job done quickly
$firstLetterFlag = false;
foreach(str_split($str[$position]) as $strLetter) {
if (
strtolower($strLetter) === strtolower($letter) &&
$firstLetterFlag === false
) {
$firstLetterFlag = true;
$strLetter = "<b>{$strLetter}</b>";
}
$message .= $strLetter;
}
$pos = stripos($str[$position], $letter);
$message .= " position {$pos}<br>";
echo $message;
}
}
var_dump(str_split($word), $position, $letter, $str[$position], $test);
您应该使用嵌套的foreach
并正确插入字符串。请注意,花括号不是必需的,但更恰当地表示正在插入变量。
正如Robby Cornelissen在评论中指出的那样,他的方法比我的快得多。我错误地说正则表达式几乎总会让你失望。正如他正确指出的那样,这是一种概括。像preg_replace
这样的正则表达式函数比对应的str_replace
慢一些。然而,在我的回答中,更多的代码通常比较少的代码运行得更慢,就像在Robby中那样。无论如何,我们谈论的是纳秒。带来速度是我的错误,因为在这种情况下它可以忽略不计。
现在,我承诺更清洁的代码。在玩了一下之后,很快就发现这不是递归函数的最佳用例。然而,函数式编程总是优于程序性的,正如Linus Torvalds所说,“如果你需要超过3级的缩进,那么无论如何你都应该搞砸了,并且应该修复你的程序。”
<?php
$word = "conrad";
$str = ["Concept", "Support", "Design", "Fresh", "Aged","Glad"];
$test = implode(" ", $str)";
echo "{$test}<br />;
function findLetter($testWord = "", $wordList = [])
{
foreach(str_split($testWord) as $position => $letter) {
if(stripos($wordList[$position], $letter) !== false) {
echo buildMessage($letter, $wordList[$position]);
}
}
}
function buildMessage($letter, $word, $firstLetterFlag = false)
{
$message = "{$letter} found in ";
foreach(str_split($word) as $wordLetter) {
if (
strtolower($wordLetter) === strtolower($letter) &&
$firstLetterFlag === false
) {
$firstLetterFlag = false;
$wordLetter = "<b>{$wordLetter}</b>";
}
$message .= $wordLetter;
}
$pos = stripos($word, $letter);
return "{$message} position {$pos}<br>";
}
findLetter($word, $str);