如何使用 preg_replace 枚举项目?
我需要在文档图像之前和之后添加一些标签并立即对它们进行编号。 HTML 文档代码是:
....
<img src="http://img.example.com/img/mage1.jpg" alt="sometile"> <br>
<img src="http://img.example.com/img/image72.jpg" alt="sometile"> <br>
<img src="http://img.example.com/img/imagstr.jpg" alt="sometile"> <br>
<img src="http://img.example.com/img/image.jpg" alt="sometile"> <br>
<img src="http://img.example.com/img/imgger.gif" alt="sometile"> <br>
<img src="http://img.example.com/img/somepic.png" alt="sometile"> <br>
我需要这样的结果代码
<div><a name="#pic1"></a><img src="http://img.example.com/img/mage1.jpg" alt="sometile"></div>
<div><a name="#pic2"></a><img src="http://img.example.com/img/image72.jpg" alt="sometile"> </div>
<div><a name="#pic3"></a><img src="http://img.example.com/img/imagstr.jpg" alt="sometile"> </div>
<div><a name="#pic4"></a><img src="http://img.example.com/img/image.jpg" alt="sometile"> </div>
<div><a name="#pic5"></a><img src="http://img.example.com/img/imgger.gif" alt="sometile"> </div>
<div><a name="#pic6"></a><img src="http://img.example.com/img/somepic.png" alt="sometile"> </div>
I need to add some tags before and after images on document and numerate them at once.
HTML Document code is:
....
<img src="http://img.example.com/img/mage1.jpg" alt="sometile"> <br>
<img src="http://img.example.com/img/image72.jpg" alt="sometile"> <br>
<img src="http://img.example.com/img/imagstr.jpg" alt="sometile"> <br>
<img src="http://img.example.com/img/image.jpg" alt="sometile"> <br>
<img src="http://img.example.com/img/imgger.gif" alt="sometile"> <br>
<img src="http://img.example.com/img/somepic.png" alt="sometile"> <br>
I need in result code like this
<div><a name="#pic1"></a><img src="http://img.example.com/img/mage1.jpg" alt="sometile"></div>
<div><a name="#pic2"></a><img src="http://img.example.com/img/image72.jpg" alt="sometile"> </div>
<div><a name="#pic3"></a><img src="http://img.example.com/img/imagstr.jpg" alt="sometile"> </div>
<div><a name="#pic4"></a><img src="http://img.example.com/img/image.jpg" alt="sometile"> </div>
<div><a name="#pic5"></a><img src="http://img.example.com/img/imgger.gif" alt="sometile"> </div>
<div><a name="#pic6"></a><img src="http://img.example.com/img/somepic.png" alt="sometile"> </div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
像往常一样,您也可以使用 DOM 来做到这一点:
As usually, you can also do that with DOM:
我不喜欢正则表达式+HTML,但这里是(我刚刚编写了一个简单的正则表达式 - 您可能已经有了一个):
(演示)
重要的部分是
preg_replace()
中的e
修饰符。请参阅“使用 'e' 修饰符的示例 #4”。编辑
正如评论中所指出的,
$i
当然不是全局变量的最佳名称。如果这将用作简单的“一次性”转换,那么可能没问题。如果不是,请将名称更改为不会发生冲突的名称。或者将其作为public static
放在类中。另外,
preg_replace_callback()
存在。它更适合,尽管我发现将函数名称作为字符串传递并评估functionName('arg')
几乎同样丑陋:)I'm not a fan of regex+HTML, but here goes (I just cooked up a simple regex—you probably already have one):
(Demo)
The important part is the
e
modifier in thepreg_replace()
. See "Example #4 Using the 'e' modifier."EDIT
As pointed out in the comments,
$i
is certainly not the best name for a global variable. If this is going to be used as a simple "one-off" transformation it may be alright. If not, change the name to something that will not conflict. Or put it as apublic static
in a class.Also,
preg_replace_callback()
exists. It's better suited, although I find the passing a function name as a string and evaluatingfunctionName('arg')
almost equally ugly :)首先,你不应该使用正则表达式来处理 HTML。
但是,在这种情况下,这很容易,因为您几乎要处理整行:
输出:
当然,如果您要使用回调,则需要将
$last_num
设置为0
再次。如果没有全局或静态变量,就无法做到这一点。但如果您只想使用回调一次,只需将
global $last_num;
更改为static $last_num = 0;
即可。然后它会永远增加,没有机会重置它——但它不会扰乱你的全局命名空间。First of all, you are not supposed to use Regexes to deal with HTML.
However, in this case it's easy as you are almost dealing with full lines:
Output:
Of course you need to set
$last_num
to0
if you are going to use the callback again.There's no way to do it without a global or static var. But if you are going to use the callback only once ever you can simply change
global $last_num;
tostatic $last_num = 0;
. Then it will increment forever with no chance of resetting it - but it won't clutter your global namespace.