I have created a blog in php. Users can post anything like a text, or a link, or youtube link. I use preg_replace() in order my code to determine when there is link or a youtube link. This is what I use:
<?php
//...code
$row['comment'] = preg_replace('@(https?://([-\w.]+[-\w])+(:\d+)?(/([\w-.~:/?#\[\]\@!$&\'()*+,;=%]*)?)?)@', '<a href="$1" target="_blank">$1</a>', $row['comment']);
$row['comment'] = preg_replace("/\s*[a-zA-Z\/\/:\.]*youtube.com\/watch\?v=([a-zA-Z0-9\-_]+)([a-zA-Z0-9\/\*\-\_\?\&\;\%\=\.]*)/i"," <object width=\"100px;\" height=\"100px;\"><param name=\"movie\" value=\"http://www.youtube.com/v/$1&hl=en&fs=1\"></param><param name=\"allowFullScreen\" value=\"true\"></param><embed src=\"http://www.youtube.com/v/$1&hl=en&fs=1\" type=\"application/x-shockwave-flash\" allowfullscreen=\"true\" width=\"400px;\" height=\"200px;\"></embed></object>",$row['comment']);
// then prints the $row['comment']
?>
my preg_replace() works fine and determines with success when there is link or a youtube link. The only problem is that when there is youtube link posted, It is displayed 2 times...probably because I gave for $row['comment'] 2 different declarations. Any idea how can I get rid of this? Is it better to combine the above 2 statements in 1? and how can I do this? Or any other "if" statement that I can use?
Any idea how to combine the above 2 statements in one?