Heim  >  Artikel  >  Backend-Entwicklung  >  读取文件,正则替换??_PHP教程

读取文件,正则替换??_PHP教程

WBOY
WBOYOriginal
2016-07-13 10:51:36838Durchsuche




读取文件,正则替换??



我要用正则替换附件中的2.xml,让它变成如下:(其实就是改标签)
也就是让


programming

Rasmus Lerdorf
Kevin Tatroe

1-56592-610-2
A greate book!


装换成


programming

Rasmus Lerdorf
Kevin Tatroe

1-56692-610-2


其实就是替换标签,值不变。
请问怎么写这个程序?先要读取文件,然后怎么正则替换?另外xml的标签怎么写成正则表达式?谢谢

我来回答

附件: 您所在的用户组无法下载或查看附件


D8888D回贴内容-------------------------------------------------------
$ftext = file_get_contents($filename);
preg_replace('要替换的字符串正则表达式', '替换的内容',$ftext);

D8888D回贴内容-------------------------------------------------------
要不多写几个str_replace ..

D8888D回贴内容-------------------------------------------------------
请问我这么写对不对:

$xml = "books.xml";
$f = fopen( 'books.xml', 'r' );
while( $data = fread( $f, 4096 ) ) { $xml .= $data; }
fclose( $f );

$xmla=preg_replace( '/', 'lee', $xml );

$xmla=preg_replace( '
', '/lee', $xmla );



echo "$xmla";

?>

D8888D回贴内容-------------------------------------------------------
请问我这么写对不对:

lxcupid 发表于 2009-6-15 13:04 [url=http://bbs.111cn.cn/redirect.php?goto=findpost&pid=1068481&ptid=128175]链接标记[img]http://bbs.111cn.cn/images/common/back.gif[/img][/url]




$xml = implode("",file('books.xml'));



$xmla=preg_replace( '/', 'lee', $xml );



$xmla=preg_replace( '
', '/lee', $xmla );







echo "$xmla";



?>


复制代码

D8888D回贴内容-------------------------------------------------------
$xml = implode("",file('books.xml'));

$xml=str_replace( array('',''), array('',''), $xml ); // 将替换为,将替换为


复制代码

D8888D回贴内容-------------------------------------------------------
谢谢,不过好像贴图少了一部分,呵呵。可是结果不是一个新的xml格式。

D8888D回贴内容-------------------------------------------------------


下面是按照楼上的代码:
$xml = implode("",file('books.xml'));
$data=str_replace( array('',''), array('',''), $xml );
// 将替换为,将替换为
echo $data;
?>
但是运行结果却是:
Rasmus Lerdorf Kevin Tatroe 1-56592-610-2

为什么不能显示一个我想要的那个新的xml文件??
附件: 您所在的用户组无法下载或查看附件
D8888D回贴内容-------------------------------------------------------
htmlentities() 输出加上这个函数

D8888D回贴内容-------------------------------------------------------
请楼上说具体些,谢谢了


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/632562.htmlTechArticle读取文件,正则替换?? 我要用正则替换附件中的2.xml,让它变成如下:(其实就是改标签) 也就是让 programming Rasmus Lerdorf Kevin Tatroe 1-5...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn