zoukankan      html  css  js  c++  java
  • preg_replace中正则表达式的反向引用

    最近遇到一个问题,要将一篇日志中的所有img中的src的取出来,然后根据src的内容重新生成src,做PHP中使用函数preg_replace(),利用了正则表达式的反向引用:

    $content = '文字文字<img src="http://www.baidu.com/img/baidu_logo_jr_1008_qx.gif>;其他文字&lt;img src="http://list.image.baidu.com/t/image_category/res/Gangtai/Ye_Xuan.jpg" alt="" width="95" height="115" >;';

    function img_convert($img,$param){
    $filepath = getBlogPicUrl($img); //调用getBlogPicUrl()重新生成新的src值
    $param= str_replace("\\",$param);
    $str ='img src="http://www.codebean.cn/wp-admin/'.$filepath.'" alt="" />';
    }

    function getBlogPicUrl($img){
    //重新生成src值的代码
    }

    $content = preg_replace("/\/ie","img_convert('$2','$1$3')",$content);

    这里使用了正则的反向引用,在调用img_convert()函数时,传给img_convert()的即是正则表达式的分别匹配三个()的值,分别按顺序为$1,$2,$3,也可是\\1,\\2,\\3 这种形式.

    另外我在preg_replace的替换部分直接使用了函数img_convert(),这需要在正则表达式中加个参数e。

  • 相关阅读:
    Trapping Rain Water
    Construct Binary Tree from Preorder and Inorder Traversal
    Flatten Binary Tree to Linked List
    Permutations II
    Unique Paths II
    Path Sum II
    Unique Binary Search Trees II
    evdev module-----uinput.py
    evdev module-----events.py
    evdev module-----device.py
  • 原文地址:https://www.cnblogs.com/codebean/p/2059908.html
Copyright © 2011-2022 走看看