Doeroper
Doeroper

Reputation: 21

How to preg_replace?

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</some_my_tag>. How to do it in PHP?

Upvotes: 2

Views: 382

Answers (3)

shamittomar
shamittomar

Reputation: 46692

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);

Upvotes: 3

Christophe
Christophe

Reputation: 4828

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

Upvotes: 1

joebert
joebert

Reputation: 2663

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

Upvotes: 0

Related Questions