如何过滤 link url http:// 和 www with preg replace
how to filter link url http:// and www with preg replace
我使用 preg_replace 做了一些功能。代码是获取它的预览。
我做了这个代码
$strings = htmlspecialchars_uni($thread['soc_instagram']);
$searchs = array('~(?:https://instagram\.com/p/)?([a-zA-Z0-9_\-+?:]+)~');
$replaces = array('https://instagram.com/p//media/?size=l');
$soc_instagram = preg_replace($searchs,$replaces,$strings);
如果我 post 一个带有此 url https://instagram.com/p/BarUcqwht_u
的 instagram,代码将完美运行
它会生成代码
https://instagram.com/p/BaQsAubg6H3/media/?size=l
但问题是当我尝试在 url 中添加 WWW 时,像这样 https://www.instagram.com/p/BarUcqwht_u 代码将产生错误字符串
结果会是这样
https://instagram.com/p/https:/media/?size=l//https://instagram.com/p/www/media/?size=l.https://instagram.com/p/instagram/media/?size=l.https://instagram.com/p/com/media/?size=l/https://instagram.com/p/p/media/?size=l/https://instagram.com/p/BarUcqwht_u/media/?size=l/
我尝试在我的 preg_replace 代码中添加 WWW,但结果会像这样
https://www.instagram.com/p/https:/media/?size=l//https://www.instagram.com/p/instagram/media/?size=l.https://www.instagram.com/p/com/media/?size=l/https://www.instagram.com/p/p/media/?size=l/https://www.instagram.com/p/BaQsAubg6H3/media/?size=l
任何帮助都会很好,谢谢
在协议后添加www.
,使其成为可选的。 preg_replace
也不需要数组,字符串可以正常工作。
$strings = 'https://www.instagram.com/p/BarUcqwht_u';
$searchs = '~(?:https://(?:www\.)?instagram\.com/p/)?([a-zA-Z0-9_\-+?:]+)~';
$replaces = 'https://instagram.com/p//media/?size=l';
$soc_instagram = preg_replace($searchs,$replaces,$strings);
echo $soc_instagram;
您当前的实现是用 URL 替换您的角色 class 中未列出的字符。请参阅 https://regex101.com/r/PXb2h1/2/ 以了解直观表示。
我使用 preg_replace 做了一些功能。代码是获取它的预览。
我做了这个代码
$strings = htmlspecialchars_uni($thread['soc_instagram']);
$searchs = array('~(?:https://instagram\.com/p/)?([a-zA-Z0-9_\-+?:]+)~');
$replaces = array('https://instagram.com/p//media/?size=l');
$soc_instagram = preg_replace($searchs,$replaces,$strings);
如果我 post 一个带有此 url https://instagram.com/p/BarUcqwht_u
的 instagram,代码将完美运行它会生成代码
https://instagram.com/p/BaQsAubg6H3/media/?size=l
但问题是当我尝试在 url 中添加 WWW 时,像这样 https://www.instagram.com/p/BarUcqwht_u 代码将产生错误字符串
结果会是这样
https://instagram.com/p/https:/media/?size=l//https://instagram.com/p/www/media/?size=l.https://instagram.com/p/instagram/media/?size=l.https://instagram.com/p/com/media/?size=l/https://instagram.com/p/p/media/?size=l/https://instagram.com/p/BarUcqwht_u/media/?size=l/
我尝试在我的 preg_replace 代码中添加 WWW,但结果会像这样
https://www.instagram.com/p/https:/media/?size=l//https://www.instagram.com/p/instagram/media/?size=l.https://www.instagram.com/p/com/media/?size=l/https://www.instagram.com/p/p/media/?size=l/https://www.instagram.com/p/BaQsAubg6H3/media/?size=l
任何帮助都会很好,谢谢
在协议后添加www.
,使其成为可选的。 preg_replace
也不需要数组,字符串可以正常工作。
$strings = 'https://www.instagram.com/p/BarUcqwht_u';
$searchs = '~(?:https://(?:www\.)?instagram\.com/p/)?([a-zA-Z0-9_\-+?:]+)~';
$replaces = 'https://instagram.com/p//media/?size=l';
$soc_instagram = preg_replace($searchs,$replaces,$strings);
echo $soc_instagram;
您当前的实现是用 URL 替换您的角色 class 中未列出的字符。请参阅 https://regex101.com/r/PXb2h1/2/ 以了解直观表示。