ragulka
ragulka

Reputation: 4342

Find content inside square brackets with PHP and replace (regex?)

I need to convert this:

[video=http://example.com/video.flv]

Into this:

<a href="http://example.com/myVideo.flv" id="player"></a>

Using PHP - probably regex. A string can contain many of these 'code blocks' and I need to give each video a unique ID. How can I do this? I am sorry, I really don't know much about regex.

Upvotes: 1

Views: 1410

Answers (2)

Landon
Landon

Reputation: 4108

Well if they are all standard in their format, why not just use str_replace()?

$s = '[video=http://www.site.com/video.flv]';
$url = str_replace(array('[video=',']'),array('',''),$s);
echo '<a href="'.$url.'">click me!</a>';

Upvotes: 2

Marc B
Marc B

Reputation: 360762

$cnt = 0;
preg_replace('/\[.*?=(.*?)]/e', '<a href="$1" id="video' . $cn++ . '">$1</a>', $text);

not tested, will probably blow up and steal your dog, etc...

Upvotes: 1

Related Questions