当前位置: 编程技术>php
微博开发之@替换示例代码
来源: 互联网 发布时间:2014-10-08
本文导语: 举个例子,微博开发之@替换: @ponyma可能被解析成了"@pony"+"ma" 代码: 代码示例: $users['pony'] = '马化腾'; $users['ponyma'] = '坡泥马'; $users['ponyli'] = '坡泥李'; $text = "@pony:特别声明,@ponyli@ponyma@ponywong@ponylao什么...
举个例子,微博开发之@替换:
@ponyma可能被解析成了"@pony"+"ma"
代码:
代码示例:
$users['pony'] = '马化腾';
$users['ponyma'] = '坡泥马';
$users['ponyli'] = '坡泥李';
$text = "@pony:特别声明,@ponyli@ponyma@ponywong@ponylao什么的都不是我";
preg_match_all('/@w+/', $text, $matches);
if(is_array($matches[0]) && !empty($matches[0])){
$replaces = array_combine($matches[0], $matches[0]);
}
foreach($users as $userid=>$username){
$replaces['@'.$userid] = "{$username}";
}
$html = strtr($text, $replaces);
echo $html;
$users['ponyma'] = '坡泥马';
$users['ponyli'] = '坡泥李';
$text = "@pony:特别声明,@ponyli@ponyma@ponywong@ponylao什么的都不是我";
preg_match_all('/@w+/', $text, $matches);
if(is_array($matches[0]) && !empty($matches[0])){
$replaces = array_combine($matches[0], $matches[0]);
}
foreach($users as $userid=>$username){
$replaces['@'.$userid] = "{$username}";
}
$html = strtr($text, $replaces);
echo $html;
结果: