tags:

views:

37

answers:

4

I want to replace something like http://www.somesite.com/videos/video23.mp4 with something like <some_my_tag>http://www.somesite.com/videos/video23.mp4&lt;/some_my_tag&gt;. How to do it in PHP?

A: 

Everything except for .mp4

Doeroper
don't answer your own question, rather add a comment to it
krike
A: 
preg_replace('#http://(.+?)\.mp4#i', '<tab>http://$1.mp4&lt;/tag&gt;', $text)
joebert
exactly, but you then asume there are only links ending with .mp4
krike
+1  A: 

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 -> http://stackoverflow.com/questions/2738555/highlighting-search-results-in-php-mysql/2739238#2739238

krike
+3  A: 

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&lt;/some_my_tag&gt;', $Str);
shamittomar