2013-08-31 14 views
-1

我希望能夠從preg_replace函數或str_replace函數中獲取數據。例如,我想轉換任何以@開頭的字符串。但我想提取以@開頭的字符串,並將其放入我的數據庫!那麼我怎樣才能提取以@開頭的特定單詞呢?如何從preg_replace提取數據

這裏是我的代碼:

$string = "@james is awespome"; 

$convert = preg_replace('@','<a href="#">@$1</a>','$string'); 

mysql_query(insert stuff); 

我希望能夠插入@詹姆斯或者詹姆斯在數據庫

+1

嘗試'preg_match'代替。 – elclanrs

+0

這個問題如何與mysql和mysqli相關? –

+0

@YourCommonSense即時通訊使用mysqli_query將數據插入我的數據庫fyi – codetastic

回答

0

使用preg_match代替:

$string = "@james is awespome"; 
preg_match('/@([A-Za-z0-9_]{1,15})/', $string, $convert); 
//matches @<string of length upto 15 chars> 
echo $convert[0]; 

輸出:

@james 

Demo!

+0

其工作! Thaks – codetastic

+0

@codetastic:很高興能幫到你。 –

0
<?php 
$string = "@james is awespome"; 
$convert = preg_replace('/(@.*?)\s.*/','$1', $string); 
print $convert; 

打印@james

+0

非常感謝它的工作! – codetastic