douju2331 2015-09-25 08:26
浏览 65

使用正则表达式替换内部链接,但如果它们是动态的,则不是,即包含php回显

I am going through a project and replacing internal links to go through a url function. I have created a bit of regex which does what I need, however I don't want it to do anything with dynamic links, as I'm going to go through and do them manually.

My regex is:

(href|src)=(?:'|")\/(?!http)(.+?)(?:'|")

My replace is:

$1="<?php echo url('$2'); ?>"

This works fine for:

<link rel='stylesheet' type='text/css' href='/public/css/print.css' media="print"/>
<link rel='stylesheet' type='text/css' href='http://example.com/public/css/print.css' media="print"/>

I want it to ignore the following though:

<link rel='stylesheet' type='text/css' href='/public/css/print<?php echo 1; ?>.css' media="print"/>

Bonus points if someone can provide a seperate regex and replace which takes the echo and places it in a string concatination of the url function! Something along the lines of

$1="<?php echo url('$2' . $3 . '$4'); ?>"
  • 写回答

2条回答 默认 最新

  • douhuan6305 2015-09-25 08:35
    关注

    (?:'|")\/(?!http)([^<]+?)(?:'|")

    will probably be good enough for the first part. Ie, replace the . with [^<] to match only strings that don't contain an opening angle bracket.

    This might work ok for the second part

    (?:'|")\/(?!http)([^<]+?)(<\?php[^>]*?>)?([^<]+?)(?:'|")

    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大