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

How to Remove URLs From a String in PHP

I printed the values ($content, $length) for the 4 examples from step 2 in turn and called the API; the results matched expectations

1. In channel product publishing, for the Weibo channel, when publishing an article of type: link (link), the Weibo platform responds with an error: status code: 400, error code: 20012, error message: Text too long, please input text less than 140 characters!, as shown in Figure 1

In channel product publishing, for the Weibo channel, when publishing an article of type: link (link), the Weibo platform responds with an error: status code: 400, error code: 20012, error message: Text too long, please input text less than 140 characters!
Figure 1
Plaintext

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. After multiple tests, I determined the rule: the string length (with URLs already removed) must contain at most 130 characters. One Chinese character counts as one length, and one English letter/number counts as half a length. As shown in Figure 2

After multiple tests, I determined the rule: the string length (with URLs already removed) must contain at most 130 characters. One Chinese character counts as one length, and one English letter/number counts as half a length.
Figure 2
Plaintext
文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章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. Because the Weibo platform’s API documentation requires the Weibo content field value to include a URL, the channel publishing API synchronously requires the content field value to include both the content and the URL. As shown in Figure 3

Because the Weibo platform's API documentation requires the Weibo content field value to include a URL, the channel publishing API synchronously requires the content field value to include both the content and the URL.
Figure 3

4. Therefore, to implement synchronous validation in the channel publishing API, I need to remove the URL from the content field value and then calculate the string length. I searched Google for: php delete url in string, and found the search result PHP – Remove URLs from string at https://gist.github.com/madeinnordeste/e071857148084da94891 , as shown in Figure 4

Therefore, to implement synchronous validation in the channel publishing API, I need to remove the URL from the content field value and then calculate the string length. I searched Google for: php delete url in string, and found the search result PHP - Remove URLs from string at https://gist.github.com/madeinnordeste/e071857148084da94891
Figure 4

5. In the model’s validation rule method, I remove the URL from the article content to get the string length. One Chinese character counts as two lengths, so the article content can contain at most 130 characters (checking if the length is greater than 260). To get the string length, you can refer to: https://www.shuijingwanwq.com/2015/11/03/725/

PHP

    /**
     * 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. I printed the values ($content, $length) for the 4 examples from step 2 in turn and called the API; the results matched expectations, as shown in Figure 5

I printed the values ($content, $length) for the 4 examples from step 2 in turn and called the API; the results matched expectations
Figure 5
Plaintext
文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章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. I discovered a bug: when a letter/number appears immediately before the URL, printing the values ($content, $length) for the 2 examples in turn shows that the URL is not removed, which does not match expectations

Plaintext
文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容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. Reference URL: https://stackoverflow.com/questions/24588470/php-remove-url-from-string . After comparative analysis, I decided to remove: \b. I printed the values ($content, $length) for the 2 examples, and the URL was successfully removed, matching expectations

 

PHP

$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

Plaintext
文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容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 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系