开发者

How to preg_replace?

开发者 https://www.devze.com 2023-01-12 18:45 出处:网络
I want to replace something like http://www.somesite.com/videos/video23.mp4 with something like <some_my_tag>http开发者_如何学Go://www.somesite.com/videos/video23.mp4</some_my_tag>. How to

I want to replace something like http://www.somesite.com/videos/video23.mp4 with something like <some_my_tag>http开发者_如何学Go://www.somesite.com/videos/video23.mp4</some_my_tag>. How to do it in PHP?


As you said that "Everything except .mp4" can change in the URL, then you can use this:

$NewStr = preg_replace('#http://(.+?)\.mp4#i', '<some_my_tag>http://$1.mp4</some_my_tag>', $Str);


like this:

$url = "http://www.somesite.com/videos/video23.mp4";
$output = preg_replace('/((?:http|https):\/\/[a-z0-9\/\?=_#&%~-]+(\.[a-z0-9\/\?=_#&%~-]+)+)|(www(\.[a-z0-9\/\?=_#&%~-]+){2,})/', '<some_my_tag>$1</some_my_tag>', $url);

The regex pattern will allow you to find any kind of url's

I answered a (more or less) similar questions here -> highlighting search results in php/mysql


preg_replace('#http://(.+?)\.mp4#i', '<tab>http://$1.mp4</tag>', $text)
0

精彩评论

暂无评论...
验证码 换一张
取 消