开发者

PHP preg_replace - Regular expression not including a phrase

$test = "<div><b><i>#uniquetag#</b></i></div> <div>Keep this</div>";

$test = preg_replace("/<div(.*)#uniquetag#(.*)<\/div>/i", "#uniquetag#", $test);

I want the result to be

$test = "#uniquetag# <div>Keep this</div>";

But it returns

$test = "#uniquetag#";

I think I know why. (.*) is greedy and it extend the search till the end. But I can't figure out what is the correct way to do it.

Update:

Special thanks to ghostdog74. Old problem solved. A new problem is experienced....

$test = "<div></div> <div><b><i>#uniquetag#</b></i></div> <div>Keep this</div>";

$test = preg_replace("/<div(.*)#uniquetag#(.*?)<\/div>/i", "#uniquetag#", $test);

Expected result is

$test = "<div></div> #uniquetag# <div>Keep this</div>";

But it turns out to be

$test = "#uniquetag# <div>Keep this</div>";

Again, I believe that's because of the first (.).开发者_Python百科 Changing it to (.?) won't help also. Need to think of a way to exclude .


change (.*) to (.*?)


In the majority of cases I'd strongly recommend using an HTML parser (such as this one) to get these links. Using regular expressions to parse HTML is going to be problematic since HTML isn't regular and you'll have no end of edge cases to consider.

See here for more info.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜