没有不值得去解决的问题,也没有不值得去学习的技术!

在 PHP 中从字符串中删除 URL 的实现

依次打印出步骤 2 中的 4 个示例的值($content、$length),调用接口,符合预期
1、在渠道发布产品中,微博渠道,发布文章类型:链接(链接)的文章,微博平台响应错误:status code: 400, error code: 20012, error message: Text too long, please input text less than 140 characters!,如图1
在渠道发布产品中,微博渠道,发布文章类型:链接(链接)的文章,微博平台响应错误:status code: 400, error code: 20012, error message:  Text too long, please input text less than 140 characters!
图1


PS E:\wwwroot\channel-pub-api> ./yii pub-article-queue/run --verbose=1 --isolate=1 --color=0
2020-02-10 16:46:03 [pid: 550884] - Worker is started
2020-02-10 16:46:04 [81] common\jobs\PubArticleJob (attempt: 1, pid: 550884) - Started
2020-02-10 16:46:04 [81] common\jobs\PubArticleJob (attempt: 1, pid: 550884) - Error (0.547 s)
> yii\web\ServerErrorHttpException: Weibo's micro-connected web application interface HTTP request, third party sharing
a link to Weibo failed, status code: 400, error code: 20012, error message:  Text too long, please input text less than
140 characters!
2020-02-10 16:46:04 [pid: 550884] - Worker is stopped (0:00:01)


2、经过多次测试,得出规则:字符串长度(已经删除掉 URL),内容只能包含至多130个字符。一个汉字表示一个长度,一个英文字母/数字表示半个长度。如图2
经过多次测试,得出规则:字符串长度(已经删除掉 URL),内容只能包含至多130个字符。一个汉字表示一个长度,一个英文字母/数字表示半个长度。
图2


文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章https://wjdev.chinamcloud.cn/www/wwwwss/wwwwww.html
10000(发布成功)
130(字符串长度,已经删除掉 URL) 181(字符串长度,保留 URL) 130(weibo,字符串长度,已经删除掉 URL)

文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章1内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章https://wjdev.chinamcloud.cn/www/wwwwss/wwwwww.html
202153(发布失败)
131(字符串长度,已经删除掉 URL) 182(字符串长度,保留 URL) 131(weibo,字符串长度,已经删除掉 URL)

文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章11111111内容文章内容文章内容文章内容文章https://wjdev.chinamcloud.cn/www/wwwwss/wwwwww.html
10000(发布成功)
134(字符串长度,已经删除掉 URL) 185(字符串长度,保留 URL) 130(weibo,字符串长度,已经删除掉 URL)

文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章https://wjdev.chinamcloud.cn/www/wwwwss/wwwwww.html内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章11111111
10000(发布成功)
134(字符串长度,已经删除掉 URL) 185(字符串长度,保留 URL) 130(weibo,字符串长度,已经删除掉 URL)


3、由于微博平台的接口文档,微博内容字段的值中要求包含 URL,因此,渠道发布的接口中,content 字段的值也是同步要求包含内容与 URL 的。如图3
由于微博平台的接口文档,微博内容字段的值中要求包含 URL,因此,渠道发布的接口中,content 字段的值也是同步要求包含内容与 URL 的。
图3
4、因此,现在要在渠道发布的接口中,实现同步验证,需要从content 字段的值中删除 URL ,再计算字符串长度。在 Google 中搜索:php delete url in string,搜索结果,PHP – Remove URLs from string,https://gist.github.com/madeinnordeste/e071857148084da94891 ,如图4
因此,现在要在渠道发布的接口中,实现同步验证,需要从content 字段的值中删除 URL ,再计算字符串长度。在 Google 中搜索:php delete url in string,搜索结果,PHP - Remove URLs from string,https://gist.github.com/madeinnordeste/e071857148084da94891
图4
5、在模型的验证规则方法中,从文章内容中删除 URL,获取字符串长度,一个汉字表示两个长度,文章内容只能包含至多130个字符(判断长度是否大于 260)。获取字符串长度,可参考:https://www.shuijingwanwq.com/2015/11/03/725/


    /**
     * Validates the content.
     * This method serves as the inline validation for content.
     *
     * @param string $attribute the attribute currently being validated
     * @param array $params the additional name-value pairs given in the rule
     */
    public function validateContent($attribute, $params)
    {
        if (!$this->hasErrors()) {
            // 检查安全域名列表中的域名是否存在于文章内容中
            $safeDomainNames = explode(",", Yii::$app->params['weiboAuth']['weiboConnectWebApp']['safeDomainName']);
            $isExist = false; // 是否存在
            foreach ($safeDomainNames as $safeDomainName) {
                $httpPos = stripos($this->$attribute, 'http://' . $safeDomainName);
                $httpsPos = stripos($this->$attribute, 'https://' . $safeDomainName);
                // 判断安全域名是否存在,如果存在,则退出循环
                if ($httpPos !== false || $httpsPos !== false) {
                    $isExist = true;
                    break;
                }
            }
            if (!$isExist) {
                $this->addError($attribute, Yii::t('error', '244009'));
            }

            // 从文章内容中删除 URL
            $content = preg_replace('/\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$]/i', '', $this->$attribute);

            // 获取字符串长度,一个汉字表示两个长度
            $length = StringHelper::strlenTwo($content);

            // 文章内容只能包含至多130个字符
            if ($length > 260) {
                $this->addError($attribute, Yii::t('error', '244010'));
            }

        }
    }


6、依次打印出步骤 2 中的 4 个示例的值($content、$length),调用接口,符合预期,如图5
依次打印出步骤 2 中的 4 个示例的值($content、$length),调用接口,符合预期
图5


文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章https://wjdev.chinamcloud.cn/www/wwwwss/wwwwww.html
10000(发布成功)
130(字符串长度,已经删除掉 URL) 181(字符串长度,保留 URL) 130(weibo,字符串长度,已经删除掉 URL)
$content:文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章
$length:260

文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章1内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章https://wjdev.chinamcloud.cn/www/wwwwss/wwwwww.html
202153(发布失败)
131(字符串长度,已经删除掉 URL) 182(字符串长度,保留 URL) 131(weibo,字符串长度,已经删除掉 URL)
$content:文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章1内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章
$length:261

文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章11111111内容文章内容文章内容文章内容文章https://wjdev.chinamcloud.cn/www/wwwwss/wwwwww.html
10000(发布成功)
134(字符串长度,已经删除掉 URL) 185(字符串长度,保留 URL) 130(weibo,字符串长度,已经删除掉 URL)
$content:文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章11111111内容文章内容文章内容文章内容文章
$length:260

文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章https://wjdev.chinamcloud.cn/www/wwwwss/wwwwww.html内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章11111111
10000(发布成功)
134(字符串长度,已经删除掉 URL) 185(字符串长度,保留 URL) 130(weibo,字符串长度,已经删除掉 URL)
$content:文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章11111111
$length:260


7、发现 Bug,当 URL 前出现字母/数字时,依次打印出 2 个示例的值($content、$length),未删除掉 URL,不符合预期


文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容00https://wjdev.chinamcloud.cn/www/wwwwss/wwwwww.html
202153(发布失败)
130(字符串长度,已经删除掉 URL) 181(字符串长度,保留 URL) 129(weibo,字符串长度,已经删除掉 URL)
$content:文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容00https://wjdev.chinamcloud.cn/www/wwwwss/wwwwww.html
$length:309

文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章aahttps://wjdev.chinamcloud.cn/www/wwwwss/wwwwww.html内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章11111111
202153(发布失败)
136(字符串长度,已经删除掉 URL) 187(字符串长度,保留 URL) 131(weibo,字符串长度,已经删除掉 URL)
$content:文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章aahttps://wjdev.chinamcloud.cn/www/wwwwss/wwwwww.html内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章11111111
$length:313


8、参考网址:https://stackoverflow.com/questions/24588470/php-remove-url-from-string ,经过对比分析,决定删除:\b,打印出 2 个示例的值($content、$length),已经删除掉 URL,符合预期  


$content = preg_replace('/\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$]/i', '', $this->$attribute);

$regex = "@(https?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?).*$)@";
echo preg_replace($regex, ' ', $string);

$content = preg_replace('/(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$]/i', '', $this->$attribute); // 删除:\b




文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容00https://wjdev.chinamcloud.cn/www/wwwwss/wwwwww.html
202153(发布失败)
130(字符串长度,已经删除掉 URL) 181(字符串长度,保留 URL) 129(weibo,字符串长度,已经删除掉 URL)
$content:文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容00
$length:258

文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章aahttps://wjdev.chinamcloud.cn/www/wwwwss/wwwwww.html内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章11111111
202153(发布失败)
136(字符串长度,已经删除掉 URL) 187(字符串长度,保留 URL) 131(weibo,字符串长度,已经删除掉 URL)
$content:文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章aa内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章11111111
$length:262


需要长期技术维护或远程问题排查?

我是拥有 15+ 年经验的 PHP / Go 后端工程师,长期关注已有系统维护、Bug 修复、性能优化、服务器排查、WordPress 网站维护和小功能迭代。

如果你的项目遇到以下情况,可以先从一次小问题排查开始合作:

  • ✅ PHP / Laravel / Yii2 老项目无人维护
  • ✅ Go / Gin 后端接口需要排查或优化
  • ✅ WordPress 网站访问慢、报错或插件冲突
  • ✅ Nginx / MySQL / Redis / Linux 服务器异常
  • ✅ CDN / Cloudflare / DNS / HTTPS 配置问题
  • ✅ 需要长期远程技术支持或兼职维护

更多介绍请查看:关于我 & 合作

微信:13980074657
邮箱:shuijingwanwq@gmail.com
Telegram:@shuijingwan
GitHub:https://github.com/shuijingwan

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理