【问题标题】:search in words on an string and change formatting在字符串上搜索单词并更改格式
【发布时间】:2014-11-12 13:35:10
【问题描述】:

我不想通过php创建高亮标签搜索功能

当我搜索单词的一部分时...整个单词被着色

例如,这是一个示例文本:

文本:英国监管机构表示,交易员使用私人在线聊天室来协调他们的买卖,从而使货币价格向有利于他们的方向转变。

当我搜索“th”时,输出是这样的:

文本:英国监管机构表示,交易员使用私人在线聊天室来协调他们的买卖,以使货币价格向他们有利。

所以...我尝试了这段代码...请帮我完成它。

这是一个算法:

$text= "British regulators say...";
foreach($word in $text)
{
  if( IS There "th" in $word)
   {
      $word2= '<b>'.$word.'</b>'
      replace($word with word2 and save in $text) 
   }
}

我怎么能用php语言呢?

【问题讨论】:

  • strpos 可能会在此处对您有所帮助,如果满足条件 - 然后将其包装在强标签中 - php.net/manual/en/function.strpos.php
  • 在下面查看我的答案
  • 这些对你有帮助吗?

标签: php search


【解决方案1】:
function highLightWords($string,$find)
{
   return preg_replace('/\b('.$find.'\w+)\b/', "<b>$1</b>", $string); 
}

用法:

$string="British regulators say traders used private online chatrooms to coordinate their buying and selling to shift currency prices in their favor.";
$find="th";
print_r(highLightWords($string,$find));

Fiddle

评论后编辑:

...我怎样才能为中间字符做呢?例如“线”

很简单,只需相应地更新正则表达式模式

return preg_replace("/\b(\w*$find\w*)\b/", "<b>$1</b>", $string); 

Fiddle

【讨论】:

  • 谢谢 :) ...我怎样才能为中间字符做呢?例如“线”
【解决方案2】:

使用 strpos() 查找您要搜索的字符的位置。然后从识别的字符位置开始读取,直到找不到任何空格为止。。

【讨论】:

    【解决方案3】:

    应该会容易得多:

    $word = "th";
    $text = preg_replace("/\b($word.*?)\b/", "<b>$1</b>", $text);
    

    【讨论】:

      【解决方案4】:

      让我们说很多。

      首先,你知道php是服务器端代码,所以,只要你不介意每次都重新加载页面或者使用ajax...

      我认为正确的方法是使用 Javascript 来实现这一点。

      说要爆炸文本你需要使用另一个函数,以确保得到什么:

      类似:

      $str = "Hello world. It's a beautiful day.";
      $words = explode(" ",$str);
      

      现在 Words var 将包含分解后的字符串。

      现在您可以循环和替换(例如),然后重新构造字符串并打印或执行其他操作。

      【讨论】:

        【解决方案5】:

        您可以使用以下代码

           <?php
        
            $string = "British regulators say traders used private online chatrooms to coordinate their buying and selling to shift currency prices in their favor";
        
             $keyword = "th";
             echo highlightkeyword($string , $keyword );
        
            function highlightkeyword($str, $search) {
                $occurrences = substr_count(strtolower($str), strtolower($search));
                $newstring = $str;
                $match = array();
        
                for ($i=1;$i<$occurrences;$i++) {
                    $match[$i] = stripos($str, $search, $i);
                    $match[$i] = substr($str, $match[$i], strlen($search));
                    $newstring = str_replace($match[$i], '[#]'.$match[$i].'[@]', strip_tags($newstring));
                }
        
                $newstring = str_replace('[#]', '<b>', $newstring);
                $newstring = str_replace('[@]', '</b>', $newstring);
                return $newstring;
        
            }
        
            ?>
        

        在这里查看https://eval.in/220395

        【讨论】:

        • 是的 O/p 是正确的,他们在 pre 标签中显示,所以没有出现粗体
        • 非常感谢,但我想把这个词加粗......不是它的一部分。
        猜你喜欢
        • 1970-01-01
        • 2011-12-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-26
        • 1970-01-01
        相关资源
        最近更新 更多