Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2566544
  • 博文数量: 245
  • 博客积分: 4125
  • 博客等级: 上校
  • 技术积分: 3113
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-25 23:56
文章分类

全部博文(245)

文章存档

2015年(2)

2014年(26)

2013年(41)

2012年(40)

2011年(134)

2010年(2)

分类: PHP

2013-05-03 09:43:17

问题

点击(此处)折叠或打开

  1. Need to remove "&page-X" from a string if its there.X being a number of course

答案

点击(此处)折叠或打开

  1. Actually the basic syntax for regular expressions, as supported by preg_replace and friends, is pretty easy to learn. Think of it as a string describing a pattern with certain characters having special meaning.
  2. In your very simple case, a possible pattern is:
  3. &page-\d+
  4. With \d meaning a digit (numeric characters 0-9) and + meaning: Repeat the expression right before + (here: \d) one or more times. All other characters just represent themselves.
  5. Therefore, the pattern above matches any of the following strings:
  6. &page-0
  7. &page-665
  8. &page-1234567890
  9. Since the preg functions use a Perl-compatible syntax and regular expressions are denoted between slashes (/) in Perl, you have to surround the pattern in slashes:
  10. $after = preg_replace('/&page-\d+/', '', $before);
  11. Actually, you can use other characters as well:
  12. $after = preg_replace('#&page-\d+#', '', $before);
  13. For a full reference of supported syntax, see the PHP manual.


参考:



另外一个例子:

点击(此处)折叠或打开

  1. $content=preg_replace('#'.$v_config['tmall_url'].'?#i',$v_config['web_url'],$content);
  2. $content=preg_replace('#/\?spm=([^\&]+)\&#i','/?',$content);
  3. $content=preg_replace('#[^"]+(["\']?)#i',$v_config['web_url'].'\\1',$content);
  4. $content=preg_replace('#[^"]+["\']?#i',$v_config['web_url'],$content);
  5. $content=preg_replace('#http://[^/]+/item.htm#i',$v_config['web_url'].'item.php',$content);
  6. $content=preg_replace('#http://\w+\.mall\.taobao\.com#i',$v_config['web_url'],$content);
  7. $content=preg_replace('#\d+\.taobao\.com#i',$v_config['web_url'],$content);
  8. $content=preg_replace('#shop/view_shop.htm#i','',$content);
  9. $content=preg_replace('#/\?search=y#i','/search.php?search=y',$content);
  10. //$content=preg_replace('#shop/viewShop.htm\?#i','search.php?',$content);
  11. $content=preg_replace('#search.htm#i','search.php',$content);
  12. $content=preg_replace('#search.php\?spm=[^&]+\&#i','search.php?',$content);
  13. $content=preg_replace('#item.php\?spm=[^&]+\&#i','item.php?',$content);
  14. $content=preg_replace('#/view_page-(\d+).htm\??#i','/view.php?id=\\1&',$content);



阅读(1536) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~