使用 Reg Expression 重新格式化 RSS feed 中的图像
我正在使用 PHP (5.2) 从 MySQL 数据库创建一些 RSS 提要,专门用于我通过 AppMakr 制作的 iPhone 应用程序。
它们取自网站上的文章,其中嵌入了图像,但在提要中它们看起来不太好。我想要尝试做的是,只要
中有图像围绕它,这样它们就在自己的行上,并且不会尝试环绕文章文本。
图像的格式是这样的:
<a rel="lightbox" href="http://images.domain.comk/543/image1.jpg"><img class="imageright" src="http://images.domain.comk/543/image1.jpg" alt="" width="300" height="250" /></a>
所以基本上被 和“imageright”或“imageleft”类包围。
我想将其更改为:
<p><img src="http://images.domain.comk/543/image1.jpg" alt="" width="300" height="250" /></p>
基本上删除 href 和 imagexxxx 类以及 p 标签中的周围内容。
我认为 preg_replace 可能必须使用,但不知道我实际会用它做什么。非常感谢任何帮助。
I am creating some RSS feeds using PHP (5.2) from a MySQL db specifically for an iPhone app I am making through AppMakr.
They are taken from articles on a website which contain images embedded in them, however in the feeds they don't look great. What I want to try and do is whenever there is an image surround it in <p>
so they are on their own line and don't try to wrap around article text.
The format of a image is like this:
<a rel="lightbox" href="http://images.domain.comk/543/image1.jpg"><img class="imageright" src="http://images.domain.comk/543/image1.jpg" alt="" width="300" height="250" /></a>
So basically surrounded with a <a href>
and with a class of "imageright" or "imageleft".
What I would love to change this to is:
<p><img src="http://images.domain.comk/543/image1.jpg" alt="" width="300" height="250" /></p>
Basically removing the href and imagexxxx class and surrounding in p tags.
I am thinking preg_replace will prob have to be used, but at a loss to what I would actually use for it. Any help is very appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因此,您需要使用正则表达式进行匹配,如下所示:
然后使用替换正则表达式,如下所示:
这不是最灵活的,但它应该可以解决 preg_replace() 的问题
So you will need to use a regexp for matching like this one:
And then a replace regexp like this:
This is not the most flexible one but it should do the trick for preg_replace()
此正则表达式匹配特定 HTML 标记的开始和结束对。标签之间的任何内容都存储到第一个捕获组中。
这给了我们一个起点。现在我们需要将
替换为
PHP 在 preg_replace 中提供了一个简单的方法来执行此操作()
现在只需插入正确的值:
这是一个未经测试的示例,旨在用作模式。您应该阅读 http://www.php.net/manual/en /function.preg-replace.php 在创建解决方案之前。
This Regex matches the opening and closing pair of a specific HTML tag. Anything between the tags is stored into the first capturing group.
This gives us a starting point. Now we need to replace the
<a href></a>
with<p></p>
PHP provides an easy method to do this in preg_replace()
now just insert the correct values:
This is a non-tested example and is meant to be used as a pattern. You should read http://www.php.net/manual/en/function.preg-replace.php before creating your solution.