Home > Backend Development > PHP Tutorial > 替换结构 这个正则如何写啊

替换结构 这个正则如何写啊

WBOY
Release: 2016-06-13 13:29:37
Original
920 people have browsed it

替换结构 这个正则怎么写啊?
页面中的:

ISO前言

引言

改为



$ptext是页面内容。好像要用反向引用,不懂啊
PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->

<?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());
}

?>



Copy after login


求指导。

------解决方案--------------------
PHP code
$s = ISO前言
<div id="divPdf2" title="../2003Z/hc/z364.pdf#page=3" class="downpdf" style="cursor:hand;">引言</div>
TXT;

echo preg_replace('# title=".+?/([^ ]+)"[^>]+>([^<a href="%241">$2</a><font color="#e78608">------解决方案--------------------</font><br>
Copy after login
探讨

PHP code
$s = 页面中的:
ISO前言

]+>([^
([^ ]+) 这部分是匹配url的,一般url不存在空的
" 就是url的结尾了
[^>]+> 就是匹配不是 > 的所有东西,即那标签可能存在其它的属性了。然后才到 > 标题头的末尾

([^

大概是这样子,不知我说得明白否。。
------解决方案--------------------
不太懂这个,。。。。照葫芦画瓢写个

PHP code


$str=ISO前言
Copy after login
引言
HT; $regExp='
(\S+)'; $replace=''; echo preg_replace("/$regExp/",$replace,$str);
------解决方案--------------------
PHP code

echo preg_replace("#<div>]*title\s*=\s*(['\"])?.*?([^/]+)\#(.*?)\\1[^>]*>(.*?)</div>#i", '<div id="div_PDf"><a href="%5C%5C2@%5C%5C3">\\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>
*/ <div class="clear">
                 
              
              
        
            </div>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template