Implementation of removing URLs from strings in PHP
1. Among the products released by the channel, the Weibo channel, the type of article published: the article of the link (link), the response error of the Weibo platform: Status code: 400, error code: 20012, error message: text Too long, please input text less than 140 characters!, as shown in Figure 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. After many tests, we can get the rules: the length of the string (the URL has been deleted), and the content can only contain up to 130 characters. A Chinese character represents a length, and an English letter/number represents half a length. as shown in Figure 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. Due to the interface document of the Weibo platform, the value of the Weibo content field requires the URL to contain the URL. Therefore, in the interface released by the channel, the value of the Content field is also required to contain the content and the URL. as shown in Figure 3
4. Therefore, to achieve synchronous verification in the interface released by the channel, you need to delete the URL from the value of the content field, and then calculate the length of the string. Search in Google: php delete url in string, search results, php – remove urls from string,https://gist.github.com/madeinnordeste/e071857148084da94891, as shown in Figure 4
5. In the verification rules and methods of the model, delete the URL from the content of the article to obtain the length of the string. One Chinese character represents two lengths. The content of the article can only contain up to 130 characters (judging whether the length is greater than 260). To get the length of the string, you can refer to:https://www.shuijingwanwq.com/en/2015/11/03/10159/
/**
* 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. Print out the values of the 4 examples in step 2 ($content, $length) in turn, and call the interface, which is in line with expectations, as shown in Figure 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. When a bug appears, when the letters/numbers appear before the url, the values of 2 examples ($content, $length) are printed in turn, and the url is not deleted, which does not meet the expectations.
文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容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-stringAfter comparative analysis, it is decided to delete:\b, print out the values of 2 examples ($content, $length), and the url has been deleted, which is in line with expectations
$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




