页面中的:
<div id="divPdf1" title="../2003Z/hb/z363.pdf#page=3" class="downpdf" style="cursor:hand;">ISO前言</div>
<div id="divPdf2" title="../2003Z/hc/z364.pdf#page=3" class="downpdf" style="cursor:hand;">引言</div>
改为
<div id="div_PDf" ><a href="z363.pdf@page=3"> ISO前言</a></div>
<div id="div_PDf" ><a href="z364.pdf@page=3"> 引言</a></div>$ptext是页面内容。好像要用反向引用,不懂啊
<?php $DBserver         = "localhost";
$DBname           = "new";
$DBuser           = "root";
$DBpassword       = "";$con = mysql_connect("localhost","root","");mysql_query("set names 'gbk'");mysql_select_db("new");$query = mysql_query("select page_id,page_text from pagecontents ") or die(mysql_error() );while($rows = mysql_fetch_array($query))
{
$pid = $rows['page_id'];
$ptext = $rows['page_text']; $ptext = preg_replace('/..\/2003Z\/[\w]+/s','',$ptext);
$ptext = mysql_real_escape_string($ptext);
         $ptext = preg_replace('','',$ptext);  mysql_query("set names 'gbk'");
$sql = "update pagecontents set page_text = ('{$ptext}') where page_id = ('{$pid}')";
mysql_query($sql) or die(mysql_error());
}?>
求指导。

解决方案 »

  1.   

    本帖最后由 xuzuning 于 2012-05-30 10:13:59 编辑
      

  2.   

    请教下xuzuning,([^ ]+)"[^>]+>([^<]+) 这个表达式点解下,。。
      

  3.   


    与楼主的答案有点问题,改进一下:
    $s = <<< TXT
    页面中的:
    <div id="divPdf1" title="../2003Z/hb/z363.pdf#page=3" class="downpdf" style="cursor:hand;">ISO前言</div>
    <div id="divPdf2" title="../2003Z/hc/z364.pdf#page=3" class="downpdf" style="cursor:hand;">引言</div>
    TXT;echo preg_replace('# title="[.\w/]+/([^ ]+)"[^>]+>([^<]+)<#i', '><a href="$1">$2</a><', $s);
      

  4.   

    ([^ ]+)"[^>]+>([^<]+)([^ ]+) 这部分是匹配url的,一般url不存在空的
    " 就是url的结尾了
    [^>]+> 就是匹配不是 > 的所有东西,即那标签可能存在其它的属性了。然后才到 > 标题头的末尾([^<]+) 这个就是标签的内容,不存在 <(这个是标签的后部分) 
    大概是这样子,不知我说得明白否。。 
      

  5.   


    谢谢楼上帮忙啊,挺接近了,需要的是这个:<div id="div_PDf" ><a href="z363.pdf@page=3"> ISO前言</a></div>
    <div id="div_PDf" ><a href="z364.pdf@page=3"> 引言</a></div>再接着替换吗?
      

  6.   

    不太懂这个,。照葫芦画瓢写个$str=<<<HT
    <div id="divPdf1" title="../2003Z/hb/z363.pdf#page=3" class="downpdf" style="cursor:hand;">ISO前言</div>
    <div id="divPdf2" title="../2003Z/hc/z364.pdf#page=3" class="downpdf" style="cursor:hand;">引言</div>
    HT;
    $regExp='<div id="(\w+)" title="\.\.\/\w+\/\w+\/(\w+\.pdf)(#)page=(\d+)" class="downpdf" style="cursor:hand;">(\S+)<\/div>';
    $replace='<div id="$1"><a href="$2@page=$4">$5</div>';
    echo preg_replace("/$regExp/",$replace,$str);
      

  7.   


    嗯,多谢指点,看来html解析是很技巧性的东西学习了。
      

  8.   


    echo preg_replace("#<div[^>]*title\s*=\s*(['\"])?.*?([^/]+)\#(.*?)\\1[^>]*>(.*?)</div>#i", '<div id="div_PDf"><a href="\\2@\\3">\\4</a></div>', $s);
    /*
    页面中的:
    <div id="div_PDf"><a href="z363.pdf@page=3">ISO前言</a></div>
    <div id="div_PDf"><a href="z364.pdf@page=3">引言</a></div>
    */
      

  9.   

    $s = <<< TXT
    页面中的:
    <div id="divPdf1" title="../2003Z/hb/z363.pdf#page=3" class="downpdf" style="cursor:hand;">ISO前言</div>
    <div id="divPdf2" title="../2003Z/hc/z364.pdf#page=3" class="downpdf" style="cursor:hand;">引言</div>
    TXT;echo preg_replace_callback('# title="[.\w/]+/([^ ]+)"[^>]+>([^<]+)<#i', 'call', $s);
    function call($m)
    {
        return '><a href="' . str_replace('#', '@', $m[1]) . '">' . $m[2] . '</a><';
    }
      

  10.   

    [User:root Time:11:47:25 Path:/home/liangdong/php]$ php preg.php 
    <div id="div_PDf"><a href="z363.pdf@page=3">ISO前言</a></div>
    <div id="div_PDf"><a href="z364.pdf@page=3">引言</a></div>
    [User:root Time:11:47:26 Path:/home/liangdong/php]$ cat preg.php 
    <?php
    $str = <<<EOF
    <div id="divPdf1" title="../2003Z/hb/z363.pdf#page=3" class="downpdf" style="cursor:hand;">ISO前言</div>
    <div id="divPdf2" title="../2003Z/hc/z364.pdf#page=3" class="downpdf" style="cursor:hand;">引言</div>
    EOF;
    $str = preg_replace('/<div id="divPdf\d" title="((?-U)[^"]*\/([^#]*)#([^"]*))".*>(.*)<\/div>/isU', '<div id="div_PDf"><a href="\2@\3">\4</a></div>', $str);
    echo $str . PHP_EOL;
    ?>