Here is the current code I am using to parse the URL and it's working for all the queries in MySQL until it hits a space in the url. Then it just separates things out like this.
http://domain.com/1 http://(1).jpg
I need a work around for this.
<?php
$con = mysql_connect("localhost","----","----");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("----", $con);
$result = mysql_query("SELECT * FROM posts ORDER BY postid DESC");
while($row = mysql_fetch_array($result))
{
$str = $row['post_content'];
$str = preg_replace_callback('#(?:https?://\S+)|(?:www.\S+)|(?:\S+\.\S+)#', function($arr)
{
if(strpos($arr[0], 'http://') !== 0)
{
$arr[0] = 'http://' . $arr[0];
}
$url = parse_url($arr[0]);
// images
if(preg_match('#\.(png|jpg|gif)$#', $url['path']))
{
return '<img src="'. $arr[0] . '" />';
}
// youtube
if(in_array($url['host'], array('www.youtube.com', 'youtube.com'))
&& $url['path'] == '/watch'
&& isset($url['query']))
{
parse_str($url['query'], $query);
return sprintf('<iframe class="embedded-video" src="http://www.youtube.com/embed/%s" allowfullscreen></iframe>', $query['v']);
}
//links
return sprintf('<a href="%1$s">%1$s</a>', $arr[0]);
}, $str);
echo $str;
echo "<br />";
}
mysql_close($con);
?>