先说要求:
1、能从html中获取并替换静态资源
2、能从json中获取并替换静态资源
其中2包括了基本键值对、值为经过转换为html实体的代码和为经转换的代码
根据需求已设计了案例,并实现了部分功能,但由于最初只测试了图片后缀,以及样例也有些问题,因此加入css这个后缀进行测试时,才发现问题,然而却搞不清楚问题出在哪里,修改更是无从谈起。
以下是代码:
<?php $content1 = ' <link href="/css/style1.css" /> <h1>测试测试测试测试</h1> <img src="/images/fj1.png.jpg" alt="舒服舒服sfsf" />'; $content1 .= "<link href='/main/style2.css' /><img src='/images/fj2.jpg' alt='舒服舒服' />"; $content2 = '<link href="/css/style9.css" /> <link href="/css/style3.jpg.css" /> <h1>测试2测试2测试2测试2</h1> <img src="/images/fj3.png.jpg" alt="舒服ss舒服sf" />'; $content2 .= "<link href='/css/style4.css' /<img src='/images/fj4.bmp' alt='舒服舒服22' />"; $statics = array( 'http://www.ttt.com/css/style5.css', '/images/fj5.png', '/images/fj6.png.gif', 'http://www.ttt.com/images/fj7.jpg', ); $content = array( 'content1' => $content1, 'content2' => htmlspecialchars($content2), 'statics' => $statics ); //以下为记录到文本中的json字符串: //{"content1":"\r\n<link href=\"\/css\/style1.css\" \/>\r\n<h1>\u6d4b\u8bd5\u6d4b\u8bd5\u6d4b\u8bd5\u6d4b\u8bd5<\/h1>\r\n<img src=\"\/images\/fj1.png.jpg\" alt=\"\u8212\u670d\u8212\u670dsfsf\" \/><link href='\/main\/style2.css' \/><img src='\/images\/fj2.jpg' alt='\u8212\u670d\u8212\u670d' \/>","content2":"<link href="\/css\/style9.css" \/>\r\n<link href="\/css\/style3.jpg.css" \/>\r\n<h1>\u6d4b\u8bd52\u6d4b\u8bd52\u6d4b\u8bd52\u6d4b\u8bd52<\/h1>\r\n<img src="\/images\/fj3.png.jpg" alt="\u8212\u670dss\u8212\u670dsf" \/><link href='\/css\/style4.css' \/<img src='\/images\/fj4.bmp' alt='\u8212\u670d\u8212\u670d22' \/>","statics":["http:\/\/www.ttt.com\/css\/style5.css","\/images\/fj5.png","\/images\/fj6.png.gif","http:\/\/www.ttt.com\/images\/fj7.jpg"]} $data = redirectStaticResources(json_encode($content)); echo '<pre>'; print_r(json_decode($data, true)); exit; /** * 重定向静态资源 * @param string $content 内容 * @param string $type 内容类型(html/json) */ function redirectStaticResources($content){ /* $fp = fopen('1.txt', 'w'); fputs($fp, $content); fclose($fp); //*/ $pattern = '/(\\\?(\'|"|"))((?!http)[^\'|\"|"]*?(css|jpg|gif|png|bmp))(\\\?(\'|"|"))/i'; $replacement = '\1http:\/\/www.test.com\/\3\5'; /* preg_match_all($pattern, $content, $match); echo '<pre>'; print_r($match); exit; //*/ $content = preg_replace($pattern, $replacement, $content); return $content; }
求大佬指点!