开发者

PHP preg_replace() problem

I was wondering if you could help.

I have the line:

$desc = preg_replace("/#\d{1,3}%/", "<b>$0</b>", $desc);

Where $desc is a tweet. Im hoping to recognise hash tags with a percentage. Such as开发者_StackOverflow社区:

#100% or #25% or #1%

However the string is not getting either matched or replaced. If you could help it would be greatly appreciated.

Example:

$desc = "testing #ugp 123 fb #75% #1% #100%. (Twitter@Feb 23, 2011 6:06 PM)";
$desc = preg_replace("/#\d{1,3}%/", "<b>$0</b>", $desc);
echo $desc;

Expected output would have the 3 matching tags to be wrapped in tags, However output does not change from the original

Cheers


works for me:

$desc = "testing #ugp 123 fb #75% #1% #100%. (Twitter@Feb 23, 2011 6:06 PM)";
$desc = preg_replace("/#\d{1,3}%/", "<b>$0</b>", $desc);
echo $desc; //testing #ugp 123 fb <b>#75%</b> <b>#1%</b> <b>#100%</b>. (Twitter@Feb 23, 2011 6:06 PM)


You need to do two things:

  1. Place parentheses around the group you want to capture
  2. Change $0 to $1 ($0 matches the entire regexp)

    preg_replace("/#(\d{1,3}%)/", "<b>$1</b>", $desc);

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜