PHP使用正则表达式获取微博中的话题和对象名

yipeiwu_com6年前PHP代码库

本文实例讲述了PHP使用正则表达式获取微博中的话题和对象名的方法。分享给大家供大家参考。具体实现方法如下:

$post_content = "@jb51和@twitter在研究用#PHP#的#正则表达式#过滤话题和对象名";
$tag_pattern = "/\#([^\#|.]+)\#/";
preg_match_all($tag_pattern, $post_content, $tagsarr);
$tags = implode(',',$tagsarr[1]);
$user_pattern = "/\@([a-zA-z0-9_]+)/";
$post_content = preg_replace($user_pattern, '<a href="http://twitter.com/${1}">@${1}</a>', $post_content );
$post_content = preg_replace($tag_pattern, '<a href="http://twitter.com/search?q=#${1}">#${1}#</a>', $post_content);

PS:这里再为大家提供2款非常方便的正则表达式工具供大家参考使用:

JavaScript正则表达式在线测试工具:
http://tools.jb51.net/regex/javascript

正则表达式在线生成工具:
http://tools.jb51.net/regex/create_reg

希望本文所述对大家的php程序设计有所帮助。

相关文章

PHP 伪静态技术原理以及突破原理实现介绍

先说实现方法: inj.php: 复制代码 代码如下: <?php set_time_limit(10); $id=$_GET["id"]; $id=str_replace(" "...

curl 出现错误的调试方法(必看)

实例如下: private function httpGet($url) { $curl = curl_init(); curl_setopt($curl,...

php preg_replace替换实例讲解

php preg_replace替换实例讲解

复制代码 代码如下:mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $l...

PHP生成不重复标识符的方法

本文实例讲述了PHP生成不重复标识符的方法。分享给大家供大家参考。具体实现方法如下: 生成唯一不重复的标识我们主要是根据当前的一个时间time然后再转换在md5值,这样几乎是可以保证标签...

php下尝试使用GraphicsMagick的缩略图功能

php下尝试使用GraphicsMagick的缩略图功能

常用的图片处理工具有GD,ImageMagick,GraphicsMagick等等。GD就是个阿斗,略过不提;ImageMagick是目前最流行的图片处理工具,它的功能非常丰富;Grap...