2014-12-05 52 views
0

查找我想从我的内容中获取价值,如@img::56如何获得爆炸@img ::值在我的数据库

@img::56 => value=56mysql_query("select * from tblgallery where g_id=56");于数据库获取记录。

塔拉吴哥酒店是第一家建在吴哥神秘土地上的四星级豪华酒店。 @img :: 56理想情况下,@ youtube:https://www.youtube.com/watch?v=k4YRWT_Aldo塔拉吴哥酒店地理位置优越,距离世界遗产吴哥窟寺庙仅6公里,距离暹粒国际机场15分钟车程,几分钟漫步到吴哥国家博物馆和@img :: 41一小段车程到市中心与柬埔寨纪念品,购物和文化阵列。

我想导致

塔拉吴哥酒店是建于吴哥的神秘土地的第一个四星级的豪华酒店。 mysql_query(“select * from tblgallery where g_id = 56”)理想情况下,< iframe width =“560”height =“315”src =“// www.youtube.com/embed/k4YRWT_Aldo”frameborder =“0” allowfullscreen > </iframe >位于交通便利的Tara Angkor Hotel酒店距离世界遗产吴哥窟寺庙仅6公里,距离暹粒国际机场15分钟车程,距离吴哥国家博物馆数分钟步行路程,距离mysql_query (“select * from tblgallery where g_id = 41”)搭乘一系列柬埔寨纪念品,购物和文化,乘车前往市中心。

+0

而且......你的代码是......? – 2014-12-05 17:14:52

+1

(提示:['preg_replace_callback'](http://php.net/preg-replace-callback)可能是你需要的工具) – 2014-12-05 17:15:22

+0

(你不需要在这里使用HTML实体来显示HTML - 只需粘贴你的HTML到框中,选中它并点击代码按钮)。 – halfer 2014-12-05 17:17:57

回答

0

如果我理解你的问题,你想从字符串@img::56检索56?

如果是这样,它可以做到如下。

$string = '@img::56'; 

// Explode the string as the delimiter. 
$components = explode('::', $string); 

// Ensure the components array has two elements. 
if(count($components) < 2) { 

    // Do something here to indicate an error has occurred. 
    //Throw an exception (preferred) or emit an error 

} 

$id = $components[1]; 

// You can use even more type checking here to ensure you have the right piece of data. 
if(!ctype_digit($id)) { 

    //Throw an exception (preferred) or emit an error 

} 

// Accessing the database using a PDO object and prepared statements 

$pdo = new \PDO('mysql:host=...', 'username', 'password') 

$statement = $pdo->prepare('SELECT * FROM tblgallary WHERE g_id = :id'); 

$statement->bindParam(':id', $id, \PDO::PARAM_INT); 

if(!$statement->execute()) { 

    //Throw an exception (preferred) or emit an error 

} 

$result = $statement->fetchAll(\PDO::FETCH_ASSOC); 

// End the database connection 
$pdo = null; 

现在你已经有了id。如果你需要$id是一个整数,你可以用(int) $components[1]来代替它。

希望这会有所帮助。