2020-12-03 16:22:55 +08:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
2021-09-04 18:07:53 +08:00
|
|
|
|
2020-12-26 21:33:40 +08:00
|
|
|
/**
|
|
|
|
* This is my open source code, please do not use it for commercial applications.
|
|
|
|
* For the full copyright and license information,
|
|
|
|
* please view the LICENSE file that was distributed with this source code
|
|
|
|
*
|
|
|
|
* @author Yuandong<837215079@qq.com>
|
|
|
|
* @link https://github.com/gzydong/hyperf-chat
|
|
|
|
*/
|
2020-12-03 16:22:55 +08:00
|
|
|
|
2021-08-31 22:33:41 +08:00
|
|
|
namespace App\Helpers;
|
2020-12-03 16:22:55 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 字符串助手类
|
|
|
|
* Class StringHelper
|
2021-04-20 16:30:57 +08:00
|
|
|
*
|
2020-12-03 16:22:55 +08:00
|
|
|
* @package App\Helper
|
|
|
|
*/
|
|
|
|
class StringHelper
|
|
|
|
{
|
2021-09-04 19:13:25 +08:00
|
|
|
/**
|
|
|
|
* 替换文本中的url 为 a标签
|
|
|
|
*
|
|
|
|
* @param string $str 字符串
|
|
|
|
* @return null|string|string[]
|
|
|
|
*/
|
|
|
|
public static function formatUrlLink(string $str)
|
|
|
|
{
|
|
|
|
$re = '@((https|http)?://([-\w\.]+)+(:\d+)?(/([\w/_\-.#%]*(\?\S+)?)?)?)@';
|
|
|
|
return preg_replace_callback($re, function ($matches) {
|
|
|
|
return sprintf('<a href="%s" target="_blank">%s</a>', trim($matches[0], '"'), $matches[0]);
|
|
|
|
}, $str);
|
|
|
|
}
|
2021-08-28 16:11:38 +08:00
|
|
|
|
2021-09-04 19:13:25 +08:00
|
|
|
/**
|
|
|
|
* 从HTML文本中提取所有图片
|
|
|
|
*
|
|
|
|
* @param string $content HTML文本
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function getHtmlImage(string $content): array
|
|
|
|
{
|
|
|
|
$pattern = "/<img.*?src=[\'|\"](.*?)[\'|\"].*?[\/]?>/";
|
|
|
|
preg_match_all($pattern, htmlspecialchars_decode($content), $match);
|
|
|
|
$data = [];
|
|
|
|
if (!empty($match[1])) {
|
|
|
|
foreach ($match[1] as $img) {
|
|
|
|
if (!empty($img)) $data[] = $img;
|
|
|
|
}
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
2020-12-03 16:22:55 +08:00
|
|
|
}
|