×

php正则替换img

php正则替换img(php正则替换img中的src)

admin admin 发表于2023-04-04 15:14:14 浏览51 评论0

抢沙发发表评论

本文目录一览:

php 正则替换所有img标签并且去掉多余属性

//写的一个正则,你试试

preg_replace("/img\s*src=(\"|\')(.*?)\\1[^]*/is",'img src="$2" /', $str)

php用正则表达式替换img中src的路径。

- - 这个还要写正则! 直接用DW 就可以批量替换了!

帮你写了一个

$url ='img width="197" height="253" alt=" " src="/case/clxy/page/files/newspic/20090928084704364888.jpg" border="0" /';-php正则替换img

$ok=preg_replace('/(img.+src=\"?.+)(case\/clxy\/page\/)(.+\.\"?.+)/i',"\${1}\${3}",$url);

echo $ok;

替换后的结果为

img width="197" height="253" alt=" " src="/files/newspic/20090928084704364888.jpg" border="0" /

php正则替换

?php

$str='a href="" id="test"任意文字 img src=""/a';

$str=preg_replace('/(a[\S\s]*?href=")([^"]*?)("[\S\s]*?[\S\s]*?img[\S\s]*?src=")([^"]*?)("[\S\s]*?[\S\s]*?\/a)/',"$1$4$3$4$5",$str);-php正则替换img

echo $str;

?

这段PHP正则处理的动作是检测任意a标签内部包含任意img标签,把a标签的链接替换为img的源链接。

这里所说的任意a标签和任意img标签指的是a标签内部可以拥有任意属性,img标签亦是同理。

但a标签内只允许包含一个img标签。

php正则表达式替换图片地址

?php

/*PHP正则提取图片img标记中的任意属性*/

$str = 'centerimg src="/uploads/images/20100516000.jpg" height="120" width="120"br /PHP正则提取或更改图片img标记中的任意属性/center';-php正则替换img

//1、取整个图片代码

preg_match('/\s*img\s+[^]*?src\s*=\s*(\'|\")(.*?)\\1[^]*?\/?\s*/i',$str,$match);

echo $match[0];

//2、取width

preg_match('/img.+(width=\"?\d*\"?).+/i',$str,$match);

echo $match[1];

//3、取height

preg_match('/img.+(height=\"?\d*\"?).+/i',$str,$match);

echo $match[1];

//4、取src

preg_match('/img.+src=\"?(.+\.(jpg|gif|bmp|bnp|png))\"?.+/i',$str,$match);

echo $match[1];

/*PHP正则替换图片img标记中的任意属性*/

//1、将src="/uploads/images/20100516000.jpg"替换为src="/uploads/uc/images/20100516000.jpg")

print preg_replace('/(img.+src=\"?.+)(images\/)(.+\.(jpg|gif|bmp|bnp|png)\"?.+)/i',"\${1}uc/images/\${3}",$str);-php正则替换img

echo "hr/";

//2、将src="/uploads/images/20100516000.jpg"替换为src="/uploads/uc/images/20100516000.jpg",并省去宽和高

print preg_replace('/(img).+(src=\"?.+)images\/(.+\.(jpg|gif|bmp|bnp|png)\"?).+/i',"\${1} \${2}uc/images/\${3}",$str);-php正则替换img

?

求教php正则高手。php正则获取html内容中的所有img路径及名称,替换img的路径

正则表达式:(?=img src=\")(/.+/)(.+?\.(gif|jpg|png))(?=\")

分别取第0和第2捕获组的数据,就是你要的获取/abc/efg/qwe/1.gif和获取1.gif的数据

替换你可以用上面的正则表达式:(?=img src=\")(/.+/)(.+?\.(gif|jpg|png))(?=\")

替换成:style/images/$2

我给你一个用Java实现的上述正则表达式的例子.

import java.util.regex.Matcher;

import java.util.regex.Pattern;

public class CC {

 public static void main(String[] args) {

  String s="img src=\"/abc/efg/qwe/1.gif\"";

  String regex="(?=img src=\")(/.+/)(.+?\\.(gif|jpg|png))(?=\")";

  Pattern p=Pattern.compile(regex);

  Matcher m=p.matcher(s);

  while(m.find()){

   System.out.println(m.group());

   System.out.println(m.group(2));

  }

  System.out.println(s.replaceAll("(?=img src=\")(/.+/)(.+?\\.(gif|jpg|png))(?=\")", "style/images/$2"));-php正则替换img

 }

}

运行结果:

/abc/efg/qwe/1.gif

1.gif

img src="style/images/1.gif"

php中正则匹配img标签,并且替换了。

你看看这个吧:

?php

function change_str($string)

{

preg_match_all('|(.*)src="(.*)"(.*)|isU',$string,$main);

$newstring = "";

foreach($main[1] as $key = $value )

{

$newstring .= $value;

if (strpos($main[2][$key],".jpg") or strpos($main[2][$key],".gif") )

{

if ( strpos($main[2][$key],"http://") === false )

$main[2][$key] = "http://".$main[2][$key];

}

$newstring .= 'src="'.$main[2][$key].'"';

$newstring .= $main[3][$key];

}

return $newstring;

}

$mystr = "";//你的字符串;

echo change_str($mystr);

?