我的正则表达式无法正确替换 - php

我有这个正则表达式:/(?:(?<=(?:style=["])).*)(line-height.*?)[;"]/i

$regex = '/(?:(?<=(?:style=["])).*)(line-height.*?)[;"]/i';

preg_replace("/(?:(?<=(?:style=[\"'])).*)(line-height.*?)[;\"]/i", "HELLO", $input);

这是输入:

    <li><span style="line-height: 20.14399986267089px">500.00dkk</span></li>
<li style="color:red; line-height: 21.14399986267089px"></li>

我只想用HELLO替换“ line-height:SOMENUMBERpx”的出现(它也必须前面加上样式标签):
但我无法使其正常运行。现在,它替换了line-height属性,但也替换了我不想要的color:red。

这是我想要的输出:

<li><span style=HELLO>500.00dkk</span></li> 
<li style="color:red; HELLO"></li>

有人可以看到我在做什么吗?

参考方案

您可以在此处使用\ K。\K resets the starting point of the reported match. Any previously consumed characters are no longer included in the final match

style=.*?\Kline-height.*?(?=[;"])

试试这个。See demo.

这样可以确保仅替换line=height...,并在其后加上style=

Div单击与单选按钮相同吗? - php

有没有一种方法可以使div上的click事件与表单环境中的单选按钮相同?我只希望下面的div提交值,单选按钮很丑代码输出如下:<input id="radio-2011-06-08" value="2011-06-08" type="radio" name="radio_date&#…

使用Ajax呈现html表 - php

我想知道如何实现以下项目其实我有一个php代码,可以渲染一张桌子<table id = "oldata" class ="table table-bordered"> <thead> <tr class ="success"> <th class="…

PHP:从函数返回值并直接回显它? - php

这可能是一个愚蠢的问题,但是……的PHPfunction get_info() { $something = "test"; return $something; } html<div class="test"><?php echo get_info(); ?></div> 有没有办…

提交表单后显示模式对话框 - php

提交下载文件后,我有一张表格。我要自动而不是自动下载文件..以显示模态对话框并显示下载链接。<form name="softwareform" id="softwareform" action="../downloadlink.php" method="POST" alig…

使用XPath在PHP中提取XML - php

我有以下XML:<root> <level name="level1"> <!-- More children <level> --> </level> <level name="level2"> <!-- Some more childre…