<php
$pattern ='<imgsrc="()">';
$html = '<img id="pic" name="pic" src="aaajpg" style="width: 640px;">';
preg_match($pattern,$html,$matches);
echo $matches[1];
>
我是参考别人的代码写的,你也可以看看>
java中这样写:
String regx = "<img\\s+alt=('([^'])'|\"([^\"])\")\\s+src=('([/,\\w,\\d,\\])'|\"([/,\\w,\\d,\\])\")";
这里有字符串转义
一般用下面这个
<img\s+alt=('([^'])'|\"([^\"])\")\s+src=('([/,\w,\d,\])'|\"([/,\w,\d,\])\")
"<img\b[^<>]\bsrc[\s\t\r\n]=[\s\t\r\n][""'][\s\t\r\n](<imgUrl>[^\s\t\r\n""'<>])[^<>]/[\s\t\r\n]>"
上面的引号中的内容就是正则表达式,用 C# 的正则表达式匹配 HTML 中的地址:
public static string[] GetHtmlImageUrlList(string sHtmlText)
{
// 定义正则表达式用来匹配 img 标签
Regex regImg = new Regex(@"<img\b[^<>]\bsrc[\s\t\r\n]=[\s\t\r\n][""'][\s\t\r\n](<imgUrl>[^\s\t\r\n""'<>])[^<>]/[\s\t\r\n]>", RegexOptionsIgnoreCase);
// 搜索匹配的字符串
MatchCollection matches = regImgMatches(sHtmlText);
int i = 0;
string[] sUrlList = new string[matchesCount];
// 取得匹配项列表
foreach (Match match in matches)
sUrlList[i++] = matchGroups["imgUrl"]Value;
return sUrlList;
}
该方法返回一个字符串数组,存放的就是从 sHtmlText 字符串中取得的地址列表。
$reg="/src[=\"\'\s]+([^\"\']+)[\"\']/i";
$str="";
if(preg_match_all($reg,$str,$m)){
for($i=0;$i<count ( $m [1] );$i++){
echo $m[1][$i];
}
}
以上就是关于php 如何取img属性值 src全部的内容,包括:php 如何取img属性值 src、怎么用正则表达式获取文章内容中的<img标签和 src的路径、正则表达式 匹配img里的src和alt值等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)