我试图从一些内容的主体中提取一个特定的url模式,并将其替换为一个新形成的url。但是我遇到了我的正则表达式模式的问题,我想看看您是否可以帮助我。
下面是我用来测试的代码:
$body = '<p><img src="/file/637/view" height="540" width="640"></p>';
$pattern = '/src="/file/(0-9)+/view"/';
$pattern = '/src="/file/(.)+/view"/';
$pattern = '/"/file/[0-9]+/view"';
$pattern = '/\<img src="(.)+"(.)+"\>/';
preg_match($pattern, $body, $matches);
现在,最后一个模式将获取整个图像标记,这很棒,但我希望它提取使用“/file/(某个数字)/view”模式的所有图像url(只是url),以便我可以形成新的url,然后对它们进行字符串替换。当我在$matches变量上运行print_r时,所有其他变量都找不到任何东西。
显然,正文字符串表示我正在扫描的内容正文。有没有什么建议可以让它工作,只抓取图片的url?这将不得不工作与多个图像与包括锚标签在内的许多其他html混合的情况。
发布于 2011-07-21 04:05:18
尝试用(.*?)
替换(.)
,或者对于您的问题,请尝试以下操作
$body = '<p><img src="/file/637/view" height="540" width="640"></p>';
$pattern = '/\/file\/([0-9]+)\/view/';
preg_match($pattern, $body, $matches);
发布于 2011-07-21 04:30:53
你需要转义斜杠,我想你有一些没有转义的斜杠
试试这个:
$body = '<p><img src="/file/637/view" height="540" width="640"></p>';
$pattern = '/<img src="\/file\/([0-9]+)\/view"/'
preg_match($pattern, $body, $matches);
echo ($matches[1]);
https://stackoverflow.com/questions/6767799
复制相似问题