从 sql 数据中检测链接并仅将其中的 50 个字母显示为文本
detect links from sql data and show only 50 letters of it as text
我一直在做一个优惠券代码网站,可以修改一些设置,但是这个似乎让我很困扰...
<?php $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
$text = $item->description;
if(preg_match($reg_exUrl, $text, $url)) {
$linktext = substr($url[0], 0, 50);
$text1 = preg_replace($reg_exUrl, '<a href="'.$url[0].'" rel="nofollow"><b>'.$linktext.'...</b></a>', $text);
}
else
{
$text1= $text;
}
$modeddescription = $text ; //string retrieved from SQL ?>
<?php echo ( !empty( $item->description ) ? '<span>' . nl2br($text1) . '</span>' : t( 'theme_no_description', 'No description.' ) ); ?>
上面的代码从 SQL 输出优惠券代码的描述,它应该从 SQL 文本数据中检测 links。
它的作用是只检测第一个 link,如果该文本中有 10 个 link,则所有显示的 10 link 将是相同的...
例如,如果文本是
http://google.com and http://facebook.com are big companies.
a good example for shopping site is http://amazon.com
会输出
http://google.com and http://google.com are big companies.
a good example for shopping site is http://google.com
我希望有人能帮我解决这个问题。
我可以使用另一个代码并单独检测所有 link,但我真的不想在页面中显示 500 个字符长的 link。只想显示 less 的前 50 个字母作为 link 标题。
此代码能够检测所有单独的 link,但如何在其中添加行间距和 50 个字符限制?
$string = preg_replace( "~[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]~","<a href=\"\0\">\0</a>", $text);
echo ".$string.";?>
preg_replace_callback()
是适合这项工作的工具。这允许您进行单个 preg_
调用,然后有条件地修改替换值。
代码:(Demo)
$pattern = "~(?:f|ht)tps?://[a-z\d.-]+\.[a-z]{2,3}\S*~i";
$text = 'http://google.com
and http://facebook.com?12345678901234567890123456789012345678901234567890 are big companies.
A good example for shopping site is http://amazon.com';
echo preg_replace_callback($pattern, function($m) {
return "<a href=\"{$m[0]}\" rel=\"nofollow\"><b>" . (strlen($m[0]) > 50 ? substr($m[0], 0, 50) . "..." : $m[0]) . "</b></a>";
}, $text);
输出:
<a href="http://google.com" rel="nofollow"><b>http://google.com</b></a>
and <a href="http://facebook.com?12345678901234567890123456789012345678901234567890" rel="nofollow"><b>http://facebook.com?123456789012345678901234567890...</b></a> are big companies.
A good example for shopping site is <a href="http://amazon.com" rel="nofollow"><b>http://amazon.com</b></a>
我一直在做一个优惠券代码网站,可以修改一些设置,但是这个似乎让我很困扰...
<?php $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
$text = $item->description;
if(preg_match($reg_exUrl, $text, $url)) {
$linktext = substr($url[0], 0, 50);
$text1 = preg_replace($reg_exUrl, '<a href="'.$url[0].'" rel="nofollow"><b>'.$linktext.'...</b></a>', $text);
}
else
{
$text1= $text;
}
$modeddescription = $text ; //string retrieved from SQL ?>
<?php echo ( !empty( $item->description ) ? '<span>' . nl2br($text1) . '</span>' : t( 'theme_no_description', 'No description.' ) ); ?>
上面的代码从 SQL 输出优惠券代码的描述,它应该从 SQL 文本数据中检测 links。 它的作用是只检测第一个 link,如果该文本中有 10 个 link,则所有显示的 10 link 将是相同的... 例如,如果文本是
http://google.com and http://facebook.com are big companies.
a good example for shopping site is http://amazon.com
会输出
http://google.com and http://google.com are big companies.
a good example for shopping site is http://google.com
我希望有人能帮我解决这个问题。 我可以使用另一个代码并单独检测所有 link,但我真的不想在页面中显示 500 个字符长的 link。只想显示 less 的前 50 个字母作为 link 标题。
此代码能够检测所有单独的 link,但如何在其中添加行间距和 50 个字符限制?
$string = preg_replace( "~[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]~","<a href=\"\0\">\0</a>", $text);
echo ".$string.";?>
preg_replace_callback()
是适合这项工作的工具。这允许您进行单个 preg_
调用,然后有条件地修改替换值。
代码:(Demo)
$pattern = "~(?:f|ht)tps?://[a-z\d.-]+\.[a-z]{2,3}\S*~i";
$text = 'http://google.com
and http://facebook.com?12345678901234567890123456789012345678901234567890 are big companies.
A good example for shopping site is http://amazon.com';
echo preg_replace_callback($pattern, function($m) {
return "<a href=\"{$m[0]}\" rel=\"nofollow\"><b>" . (strlen($m[0]) > 50 ? substr($m[0], 0, 50) . "..." : $m[0]) . "</b></a>";
}, $text);
输出:
<a href="http://google.com" rel="nofollow"><b>http://google.com</b></a>
and <a href="http://facebook.com?12345678901234567890123456789012345678901234567890" rel="nofollow"><b>http://facebook.com?123456789012345678901234567890...</b></a> are big companies.
A good example for shopping site is <a href="http://amazon.com" rel="nofollow"><b>http://amazon.com</b></a>