preg_replace突然感觉好强大啊
1 <p><img align="absmiddle" src="https://img.alicdn.com/imgextra/i1/1696415374/TB28Af4dpXXXXbkXpXXXXXXXXXX_!!1696415374.jpg" style="font-size: inherit; padding: 0px; outline: 0px; border: 0px; height: auto; vertical-align: middle;"/><img align="absmiddle" src="https://img.alicdn.com/imgextra/i2/1696415374/TB2LC29dpXXXXXJXpXXXXXXXXXX_!!1696415374.jpg" style="font-size: inherit; padding: 0px; outline: 0px; border: 0px; height: auto; vertical-align: middle;"/><img align="absmiddle" src="https://img.alicdn.com/imgextra/i4/1696415374/TB25N.pdpXXXXXfXXXXXXXXXXXX_!!1696415374.jpg" style="font-size: inherit; padding: 0px; outline: 0px; border: 0px; height: auto; vertical-align: middle;"/><img align="absmiddle" src="https://img.alicdn.com/imgextra/i3/1696415374/TB2axr0dpXXXXcnXpXXXXXXXXXX_!!1696415374.jpg" style="font-size: inherit; padding: 0px; outline: 0px; border: 0px; height: auto; vertical-align: middle;"/><img align="absmiddle" src="https://img.alicdn.com/imgextra/i4/1696415374/TB2rv62dpXXXXbJXpXXXXXXXXXX_!!1696415374.jpg" style="font-size: inherit; padding: 0px; outline: 0px; border: 0px; height: auto; vertical-align: middle;"/><img align="absmiddle" src="https://img.alicdn.com/imgextra/i3/1696415374/TB2Yg27dpXXXXawXpXXXXXXXXXX_!!1696415374.jpg" style="font-size: inherit; padding: 0px; outline: 0px; border: 0px; height: auto; vertical-align: middle;"/><img align="absmiddle" src="https://img.alicdn.com/imgextra/i2/1696415374/TB2SsY_dpXXXXXTXpXXXXXXXXXX_!!1696415374.jpg" style="font-size: inherit; padding: 0px; outline: 0px; border: 0px; height: auto; vertical-align: middle;"/><img align="absmiddle" src="https://img.alicdn.com/imgextra/i2/1696415374/TB2tAD8dpXXXXabXpXXXXXXXXXX_!!1696415374.jpg" style="font-size: inherit; padding: 0px; outline: 0px; border: 0px; height: auto; vertical-align: middle;"/><img align="absmiddle" src="https://img.alicdn.com/imgextra/i3/1696415374/TB2ds61dpXXXXb8XpXXXXXXXXXX_!!1696415374.jpg" style="font-size: inherit; padding: 0px; outline: 0px; border: 0px; height: auto; vertical-align: middle;"/><img align="absmiddle" src="https://img.alicdn.com/imgextra/i2/1696415374/TB2cyZjdpXXXXaQXXXXXXXXXXXX_!!1696415374.jpg" style="font-size: inherit; padding: 0px; outline: 0px; border: 0px; height: auto; vertical-align: middle;"/><img align="absmiddle" src="https://img.alicdn.com/imgextra/i2/1696415374/TB2TUZhdpXXXXXxXXXXXXXXXXXX_!!1696415374.jpg" style="font-size: inherit; padding: 0px; outline: 0px; border: 0px; height: auto; vertical-align: middle;"/><img align="absmiddle" src="https://img.alicdn.com/imgextra/i1/1696415374/TB2C07odpXXXXXyXXXXXXXXXXXX_!!1696415374.jpg" style="font-size: inherit; padding: 0px; outline: 0px; border: 0px; height: auto; vertical-align: middle;"/><img align="absmiddle" src="https://img.alicdn.com/imgextra/i3/1696415374/TB2egT6dpXXXXa4XpXXXXXXXXXX_!!1696415374.jpg" style="font-size: inherit; padding: 0px; outline: 0px; border: 0px; height: auto; vertical-align: middle;"/><img align="absmiddle" src="https://img.alicdn.com/imgextra/i1/1696415374/TB2D3EodpXXXXXyXXXXXXXXXXXX_!!1696415374.jpg" style="font-size: inherit; padding: 0px; outline: 0px; border: 0px; height: auto; vertical-align: middle;"/><img align="absmiddle" src="https://img.alicdn.com/imgextra/i1/1696415374/TB2BDgldpXXXXXSXXXXXXXXXXXX_!!1696415374.jpg" style="font-size: inherit; padding: 0px; outline: 0px; border: 0px; height: auto; vertical-align: middle;"/><br/></p>
需要把上面这段html中的图片网址,上传到自己的服务器,并做好映射。
提取url:
preg_match_all('/<img[^>]*src\s?=\s?[\'|"](?!http:\/\/img.jinyuanbao|http:\/\/img.jybtest)([^\'|"]*)[\'|"]/is', $content, $picarr);
这段代码的意思是提取非img.jinyuanbao和img.jybtest开头的网址。提取完毕,等到数组$picarr;
1 Array 2 ( 3 [0] => Array 4 ( 5 [0] => <img align="absmiddle" src="https://img.alicdn.com/imgextra/i1/1696415374/TB28Af4dpXXXXbkXpXXXXXXXXXX_!!1696415374.jpg" 6 [1] => <img align="absmiddle" src="https://img.alicdn.com/imgextra/i2/1696415374/TB2LC29dpXXXXXJXpXXXXXXXXXX_!!1696415374.jpg" 7 [2] => <img align="absmiddle" src="https://img.alicdn.com/imgextra/i4/1696415374/TB25N.pdpXXXXXfXXXXXXXXXXXX_!!1696415374.jpg" 8 [3] => <img align="absmiddle" src="https://img.alicdn.com/imgextra/i3/1696415374/TB2axr0dpXXXXcnXpXXXXXXXXXX_!!1696415374.jpg" 9 [4] => <img align="absmiddle" src="https://img.alicdn.com/imgextra/i4/1696415374/TB2rv62dpXXXXbJXpXXXXXXXXXX_!!1696415374.jpg" 10 [5] => <img align="absmiddle" src="https://img.alicdn.com/imgextra/i3/1696415374/TB2Yg27dpXXXXawXpXXXXXXXXXX_!!1696415374.jpg" 11 [6] => <img align="absmiddle" src="https://img.alicdn.com/imgextra/i2/1696415374/TB2SsY_dpXXXXXTXpXXXXXXXXXX_!!1696415374.jpg" 12 [7] => <img align="absmiddle" src="https://img.alicdn.com/imgextra/i2/1696415374/TB2tAD8dpXXXXabXpXXXXXXXXXX_!!1696415374.jpg" 13 [8] => <img align="absmiddle" src="https://img.alicdn.com/imgextra/i3/1696415374/TB2ds61dpXXXXb8XpXXXXXXXXXX_!!1696415374.jpg" 14 [9] => <img align="absmiddle" src="https://img.alicdn.com/imgextra/i2/1696415374/TB2cyZjdpXXXXaQXXXXXXXXXXXX_!!1696415374.jpg" 15 [10] => <img align="absmiddle" src="https://img.alicdn.com/imgextra/i2/1696415374/TB2TUZhdpXXXXXxXXXXXXXXXXXX_!!1696415374.jpg" 16 [11] => <img align="absmiddle" src="https://img.alicdn.com/imgextra/i1/1696415374/TB2C07odpXXXXXyXXXXXXXXXXXX_!!1696415374.jpg" 17 [12] => <img align="absmiddle" src="https://img.alicdn.com/imgextra/i3/1696415374/TB2egT6dpXXXXa4XpXXXXXXXXXX_!!1696415374.jpg" 18 [13] => <img align="absmiddle" src="https://img.alicdn.com/imgextra/i1/1696415374/TB2D3EodpXXXXXyXXXXXXXXXXXX_!!1696415374.jpg" 19 [14] => <img align="absmiddle" src="https://img.alicdn.com/imgextra/i1/1696415374/TB2BDgldpXXXXXSXXXXXXXXXXXX_!!1696415374.jpg" 20 ) 21 22 [1] => Array 23 ( 24 [0] => https://img.alicdn.com/imgextra/i1/1696415374/TB28Af4dpXXXXbkXpXXXXXXXXXX_!!1696415374.jpg 25 [1] => https://img.alicdn.com/imgextra/i2/1696415374/TB2LC29dpXXXXXJXpXXXXXXXXXX_!!1696415374.jpg 26 [2] => https://img.alicdn.com/imgextra/i4/1696415374/TB25N.pdpXXXXXfXXXXXXXXXXXX_!!1696415374.jpg 27 [3] => https://img.alicdn.com/imgextra/i3/1696415374/TB2axr0dpXXXXcnXpXXXXXXXXXX_!!1696415374.jpg 28 [4] => https://img.alicdn.com/imgextra/i4/1696415374/TB2rv62dpXXXXbJXpXXXXXXXXXX_!!1696415374.jpg 29 [5] => https://img.alicdn.com/imgextra/i3/1696415374/TB2Yg27dpXXXXawXpXXXXXXXXXX_!!1696415374.jpg 30 [6] => https://img.alicdn.com/imgextra/i2/1696415374/TB2SsY_dpXXXXXTXpXXXXXXXXXX_!!1696415374.jpg 31 [7] => https://img.alicdn.com/imgextra/i2/1696415374/TB2tAD8dpXXXXabXpXXXXXXXXXX_!!1696415374.jpg 32 [8] => https://img.alicdn.com/imgextra/i3/1696415374/TB2ds61dpXXXXb8XpXXXXXXXXXX_!!1696415374.jpg 33 [9] => https://img.alicdn.com/imgextra/i2/1696415374/TB2cyZjdpXXXXaQXXXXXXXXXXXX_!!1696415374.jpg 34 [10] => https://img.alicdn.com/imgextra/i2/1696415374/TB2TUZhdpXXXXXxXXXXXXXXXXXX_!!1696415374.jpg 35 [11] => https://img.alicdn.com/imgextra/i1/1696415374/TB2C07odpXXXXXyXXXXXXXXXXXX_!!1696415374.jpg 36 [12] => https://img.alicdn.com/imgextra/i3/1696415374/TB2egT6dpXXXXa4XpXXXXXXXXXX_!!1696415374.jpg 37 [13] => https://img.alicdn.com/imgextra/i1/1696415374/TB2D3EodpXXXXXyXXXXXXXXXXXX_!!1696415374.jpg 38 [14] => https://img.alicdn.com/imgextra/i1/1696415374/TB2BDgldpXXXXXSXXXXXXXXXXXX_!!1696415374.jpg 39 ) 40 41 )
我们把$picar[1]中的图片连接存在oss;
https://img.alicdn.com/imgextra/i1/1696415374/TB2BDgldpXXXXXSXXXXXXXXXXXX_!!1696415374.jpg
存储在OSS上
http://img.jybtest.com/425968826991101.jpg
得到一个映射的数组$pics:
$pics = array( 'https://img.alicdn.com/imgextra/i1/1696415374/TB2BDgldpXXXXXSXXXXXXXXXXXX_!!1696415374.jpg' => 'http://img.jybtest.com/425968826991101.jpg'
);
foreach ($pics as $k => $v) {
$content = str_replace($k, $v, $content);
}
//或者
$k = array_map(function($item) {return '/'.str_replace('/', '\/', $item).'/';}, array_keys($pics));
$v = array_values($pics);
$content = preg_replace($k, $v, $content);
print_r($content);
preg_replace看出这个很方便地!
本博客不在更新!!!!
!!!我的新站地址点击这里,欢迎光顾!!!