Php搜索替换者

时间:2018-02-13 17:12:30

标签: php string

我有一个搜索字符串:$ str(像“test”之类的东西),一个换行字符串:$ wrap(像“|”之类的东西)和一个文本字符串:$ text(像“这是一个测试文本”之类的东西)

$ str是$ text in $ text。我现在想要的是一个函数,它将使用$ wrap中定义的包裹来包装$ str并输出修改后的文本(即使$ str在$ text中超过一次)。

但它不会输出整个文本,只会输出$ str之前的1-2个单词,然后输出$ str之后的单词和“......”(仅当它不是第一个或最后一个时)字)。它也应该不区分大小写。

示例:

$str = "Text"
$wrap = "<span>|</span>"
$text = "This is a really long Text where the word Text appears about 3 times Text"

输出将是:

"...long <span>Text</span> where...word <span>Text</span> appears...times  <span>Text</span>"

我的代码(Obviusly不起作用):

$tempar = preg_split("/$str/i", $text);

        if (count($tempar) <= 2) {
            $result = "... ".substr($tempar[0], -7).$wrap.substr($tempar[1], 7)." ...";
        } else {
            $amount = substr_count($text, $str);

            for ($i = 0; $i < $amount; $i++) {
                $result = $result.".. ".substr($tempar[$i], -7).$wrap.substr($tempar[$i+1], 0, 7)." ..";
            }

        }

如果您有tipp或解决方案,请随时告诉我。

3 个答案:

答案 0 :(得分:0)

如果我理解你的问题是正确的,只需要一点点内爆和爆炸魔法

$text = "This is a really long Text where the word Text appears about 3 times Text";
$arr = explode("Text", $text);
print_r(implode('&lt;span&gt;Text&lt;/span&gt;', $arr));

如果你特别需要使用HTML渲染span标签,那就这样写吧

$arr = explode("Text", $text);
print_r(implode('<span>Text</span>', $arr));

答案 1 :(得分:0)

我采取了你的方法并使其更加灵活。如果$str$wrap更改,您可以在正则表达式模式中转义问题,因此我使用了preg_quote

请注意,我添加了$placeholder以使其更加清晰,但如果您不喜欢$placeholder = "|",则可以使用[placeholder]

function wrapInString($str, $text, $element = 'span') {
    $placeholder = "[placeholder]"; // The string that will be replaced by $str
    $wrap = "<{$element}>{$placeholder}</{$element}>"; // Dynamic string that can handle more than just span
    $strExp = preg_quote($str, '/');

    $matches = [];
    $matchCount = preg_match_all("/(\w+\s+)?(\w+\s+)?({$strExp})(\s+\w+)?(\s+\w+)?/i", $text, $matches);

    $response = '';
    for ($i = 0; $i < $matchCount; $i++) {
        if (strlen($matches[1][$i])) {
            $response .= '...';
        }
        if (strlen($matches[2][$i])) {
            $response .= $matches[2][$i];
        }
        $response .= str_replace($placeholder, $matches[3][$i], $wrap);
        if (strlen($matches[4][$i])) {
            $response .= $matches[4][$i];
        }
        if (strlen($matches[5][$i]) && $i == $matchCount - 1) {
            $response .= '...';
        }
    }
    return $response;
}

$text = "text This is a really long Text where the word Text appears about 3 times Text";
string(107) "<span>text</span> This...long <span>text</span> where...<span>text</span> appears...times <span>text</span>"

要使替换大小写不敏感,可以使用i正则表达式选项。

答案 2 :(得分:0)

使用下面的patern来获取你的话和

之前和之后的1-2个单词
/((\w+\s+){1,2}|^)text((\s+\w+){1,2}|$)/i

demo

在PHP代码中,它可以是:

    $str = "Text";
    $wrap = "<span>|</span>";
    $text = "This is a really long Text where the word Text appears about 3 times Text";
    $temp = str_replace('|', $str, $wrap); // <span>Text</span>
    // find patern and 1-2 words before and after 
    // (to make it casesensitive, delete 'i' from patern)
    if(preg_match_all('/((\w+\s+){1,2}|^)text((\s+\w+){1,2}|$)/i', $text, $match)) {
      $res = array_map(function($x) use($str, $temp) { return '... '.str_replace($str, $temp, $x) . ' ...';}, $match[0]);
      echo implode(' ', $res);
}