doushizhou4477 2012-05-18 15:09
浏览 128

正则表达式使用php将文件路径替换为字符串中的文件名

I've got a css string like this :

$string = "div#test {background: url(images/test.gif); width:100px; } div#test2 {background: url(../images/test2.gif); } ";

Basically I want to able to replace everything between url () to just the filename. So that it eventually looks like :

$string = "div#test {background: url(test.gif); width:100px; } div#test2 {background: url(test2.gif); } ";

Sort of like applying basename but for relative urls and for all such instances.

Any ideas ?

  • 写回答

3条回答 默认 最新

  • dongya8378 2012-05-18 15:12
    关注

    Try this:

    EDIT: I fixed the regex

    <?php 
    
    $string = "div#test {background: url(images/test.gif); width:100px; } div#test2 {background: url(../images/test2.gif); } ";
    $output = preg_replace('/([\.\w\/]*\/)/', '', $string);
    
    var_dump($output);
    string(93) "div#test {background: url(test.gif); width:100px; } div#test2 {background: url(test2.gif); } "
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn