所以,我想在Twitter上发布的文字有时超过140个字符,因此,我需要检查长度,然后如果不到140则不进行更改或将文本分成两部分(文本和链接)并抓住文本部分并使其成为例如100个字符 - 其余部分。
然后抓住 - 现在长度为100个字符的部分 - 并将其与网址完全匹配。
怎么做?
到目前为止我的代码:
if (strlen($status) < 140) {
// continue
} else {
// 1. slice the $status into $text and $url (every message has url so
// checking is not important right now
// 2. shorten the text to 100 char
// something like $text = substr($text, 0, 100); ?
// 3. put them back together
$status = $text . ' ' . $url;
}
我应该如何更改我的代码?获取网址和文本部分时,第一部分的问题最大。
顺便说一下。在每个$ status中只有1个url,因此不需要检查多个url
比其应该更长的文本示例:
现在的情况厄瓜多尔是各种土着群体的家园,这些群体在十五世纪逐渐融入印加帝国。该地区在十六世纪被西班牙殖民,1820年作为格兰哥伦比亚的一部分实现独立,1830年它成为自己的主权国家。两个帝国的遗产都体现在厄瓜多尔种族多样化的人口中,其中大部分都是1520万人是混血儿,其次是欧洲,美洲印第安人和非洲后裔的少数民族。 https://en.wikipedia.org/wiki/Ecuador
最终应该成为:
现在的情况厄瓜多尔是各种土着群体的家园,这些群体逐渐被纳入https://en.wikipedia.org/wiki/Ecuador
答案 0 :(得分:1)
如果你可以确定URL不包含任何空格(不应该有格式良好的URL)并且它始终存在,那就试试吧:
preg_match('/^(.*)(\\S+)$/', $status, $matches);
$text = $matches[1];
$url = $matches[2];
$text = substr($text, 0, 100);
但是文本的长度可能会根据网址的长度进行调整,因此您可以使用
$text = substr($text, 0, 140-strlen($url)-1);
答案 1 :(得分:0)
$reg = '/\b(?:(?:https?|ftp|file):\/\/|www\.|ftp\.)[-A-Z0-9+&@#\/%=~_|$?!:,.]*[A-Z0-9+&@#\/%=~_|$]/i';
$string = "What is now Ecuador was home to a variety of indigenous groups that were gradually incorporated into the Inca Empire during the fifteenth century. The territory was colonized by Spain during the sixteenth century, achieving independence in 1820 as part of Gran Colombia, from which it emerged as its own sovereign state in 1830. The legacy of both empires is reflected in Ecuador's ethnically diverse population, with most of its 15.2 million people being mestizos, followed by large minorities of European, Amerindian, and African descendant. https://en.wikipedia.org/wiki/Ecuador";
preg_match_all($reg, $string, $matches, PREG_PATTERN_ORDER);
$cut_string = substr($string, 0, (140-strlen($matches[0][0])-1));
$your_twitt = $cut_string . " " . $matches[0][0];
echo $your_twitt;
// ouputs : "What is now Ecuador was home to a variety of indigenous groups that were gradually incorporated into t https://en.wikipedia.org/wiki/Ecuador"
答案 2 :(得分:0)
这可能是你想要的:
$status = 'What is now Ecuador was home to a variety of indigenous groups that were gradually incorporated into the Inca Empire during the fifteenth century. The territory was colonized by Spain during the sixteenth century, achieving independence in 1820 as part of Gran Colombia, from which it emerged as its own sovereign state in 1830. The legacy of both empires is reflected in Ecuador\'s ethnically diverse population, with most of its 15.2 million people being mestizos, followed by large minorities of European, Amerindian, and African descendant. https://en.wikipedia.org/wiki/Ecuador';
if (strlen($status) < 140) {
echo 'Lenght ok';
} else {
$totalPart = round(strlen($status)/100);
$fulltweet = array();
for ($i=0; $i < $totalPart; $i++) {
if($i==0)
{
$fulltweet[$i] = substr($status, 0,100);
}else{
$fulltweet[$i] = substr($status, $i * 100);
}
}
}
如果字符串超过140个字符,那么它将在每行100个字符的数组中将其爆炸