gpt4 book ai didi

php - 修改一个 php 限制文本函数,向其添加某种偏移量

转载 作者:可可西里 更新时间:2023-11-01 14:04:22 26 4
gpt4 key购买 nike

也许你们可以帮忙:

我有一个名为 $bio 的变量,其中包含生物数据。

$bio = "Hello, I am John, I'm 25, I like fast cars and boats. I work as a blogger and I'm way cooler then the author of the question";

我使用一组函数搜索 $bio 来搜索某个词,比方说 “author” 在该词周围添加一个 span 类,然后我得到:

$bio = "Hello, I am John, I'm 25, I like fast cars and boats. I work as a blogger and I'm way cooler then the <span class=\"highlight\">author</span> of the question";

我使用函数将文本限制为 85 个字符:

$bio = limit_text($bio,85);

问题$bio 中的 "author" 之前有超过 80 个字符。当应用 limit_text() 时,我不会看到突出显示的词作者。

我需要的是让 limit_text() 函数正常工作,在末尾添加所有包含 span 类高亮的单词。像这样:

*"This is the limited text to 85 chars, but there are no words with the span class highlight so I am putting to be continued ... **author**, **author2** (and all the other words that have a span class highlight around them separate by comma "*

希望你明白我的意思,如果不明白,请发表评论,我会尽力解释得更好。

这是我的 limit_text() 函数:

function limit_text($text, $length){ // Limit Text
if(strlen($text) > $length) {
$stringCut = substr($text, 0, $length);
$text = substr($stringCut, 0, strrpos($stringCut, ' '));
}
return $text;
}

更新:

$xturnons = str_replace(",", ", ", $xturnons);
$xbio = str_replace(",", ", ", $xbio);

$xbio = customHighlights($xbio,$toHighlight);
$xturnons = customHighlights($xturnons,$toHighlight);

$xbio = limit_text($xbio,85);
$xturnons = limit_text($xturnons,85);

customHighlights 函数添加高亮的 span 类:

function addRegEx($word){ // Highlight Words
return "/" . $word . '[^ ,\,,.,?,\.]*/i';
}
function highlight($word){
return "<span class='highlighted'>".$word[0]."</span>";
}
function customHighlights($searchString,$toHighlight){
$searchFor = array_map('addRegEx',$toHighlight);
$result = preg_replace_callback($searchFor,'highlight',$searchString);
return $result;
}

最佳答案

limit_text 函数的这种更改将获取文本,如果它比给定的 $length 长,则将其剪切。如果您将 $needle 传递给它,它将搜索第一次出现的它,并以它结束您的句子。

此外,如果文本在其实际长度之前被剪切,它会向其添加 $addition,同时仍保留 $length 个字符的限制。

我已经包含了一个用法和一个基于你给出的示例输出:

<?php
/**
* $text - The text to cut from
* $length - The amount of characters that should be returned
* $needle - If needle is given and found in the text, and it is
* at least $length far from the start of the string - it will end the sentence with it.
* $addition - If the sentence was cut in the middle, will add it to the end of it.
**/
function limit_text($text, $length, $needle="", $addition="...") {
if(strlen($text) > $length) {
$length -= strlen($addition);

$start = 0;
$trimLast = true;
if (!empty($needle)) {
$needleStart = strpos($text, $needle);

if ($needleStart > $length) {
$length -= strlen($needle);
$start = $needleStart + strlen($needle) - $length;
$trimLast = false;
}
}

$stringCut = substr($text, max(0, $start), $length);
if ($start > 0) {
$stringCut = substr($stringCut, strpos($stringCut, ' ')+1);
}
if ($trimLast) {
$lastWhitespace = strrpos($stringCut, ' ');
$stringCut = substr($stringCut, 0, $lastWhitespace);
}

// split into words (so we won't replace words that contain it in the middle)
// and wrap $needle with <span class="highlighted"></span>
if (!empty($needle)) {
$words = explode(" ", $stringCut);
$needles = array_keys($words, $needle);
foreach ($needles as $needleKey) {
$words[$needleKey] = "<span class=\"highlighted\">$needle</span>";
}

$stringCut = implode(" ", $words);
}
$text = $stringCut.$addition;
}
return $text;
}

$bio = "Hello, I am John, I'm 25, I like fast cars and boats. I work as a blogger and I'm way cooler then the author of the question";
$text = limit_text($bio, 85, "author");
var_dump($text);

输出:

string (111) "fast cars and boats. I work as a blogger and I'm way cooler then the <span class="highlighted">author</span>..."

关于php - 修改一个 php 限制文本函数,向其添加某种偏移量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11129461/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com