0

我希望能够从 preg_replace 函数或 str_replace 函数中获取数据。例如我想转换任何以@开头的字符串。但是我想提取以@开头的字符串并将其放入我的数据库中!那么我怎样才能提取以@开头的特定单词呢?

这是我的代码:

$string = "@james is awespome";

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

mysql_query( insert stuff);

我希望能够在数据库中插入@james 或 james

4

2 回答 2

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

印刷@james

于 2013-08-31T07:54:15.247 回答
1

改用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

演示!

于 2013-08-31T07:54:31.453 回答