douxia6163 2017-01-09 18:38
浏览 22
已采纳

PHP Shortcode正则表达式问题

Hi so I need help in taking a block of html which contains existing shortcode for an old arbitrary system. Taking the code below and and using PHP change so the following :

[CDC](http://www.cdc.gov/)

would be transformed into this :

<a href="http://cdc.gov">CDC</a> 

Any ideas on how i could achive this? There could be multiple instances in one block of code also. If anybody can help , I'd be grateful - thank you!!

  • 写回答

2条回答 默认 最新

  • dsjbest2014 2017-01-09 19:05
    关注

    The solution using preg_replace function with specific regex pattern:

    $block = "Two excellent websites outlining the major precautions are: [some text](www.cdc.gov) and [who's next](www.who.int) which are the official sites ...";
    
    $block = preg_replace("/\[([^]]+)\]\(([^)]+)\)/", '<a href="$2">$1</a>', $block);
    
    print_r($block);
    

    The output(from source code):

    Two excellent websites outlining the major precautions are: <a href="www.cdc.gov">some text</a> and <a href="www.who.int">who's next</a> which are the official sites ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏