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

yipeiwu_com4年前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 截取GBK文档某个位置开始的n个字符方法

cut.php: #!/usr/bin/php <?php define('INPUT_FILE', 't.txt'); define('OUTPUT_FILE', '...

Fedora下安装php Redis扩展笔记

一、安装编译工具 复制代码 代码如下: yum install wget make gcc gcc-c++ zlib-devel openssl openssl-devel pcre-d...

PHP创建桌面快捷方式的实例代码

复制代码 代码如下:$shortCut = "[InternetShortcut]URL=//www.jb51.netIDList=[{000214A0-0000-0000-C000-0...

PHP 引用文件技巧

我们知道,java中有包的概念,而.NET中则有更加方便的DLL程序集引用的概念,通过这些以打包形式组合在一起的对象集合,我们可以很方便的在自己的类中引用其他地方定义的类或其他对象,但由...

PHP学习笔记(二) 了解PHP的基本语法以及目录结构

PHP学习笔记(二) 了解PHP的基本语法以及目录结构

通过这节课, 1.了解AppServ的目录结构 2.我们可以了解PHP的基本语法结构 1 我的AppServ安装目录是E盘: ①运用命令行的方式去操作apache服务器 apache服...