开发者

How to cut this tag out in php?

I want cut out everything inside (tags included) from text, for ex:

[url=http://example.com]something[/url]

but with one condition, inside of [url] tag there would be [img] tag. So final sentence to cut:

[url=http://example.com]anything[img]something[/url]

can anyone help? im bad with regular expressi开发者_如何学编程ons, or there is easier way?


How about this:

<?php

ini_set('display_errors', 1);
ini_set('error_reporting', E_ALL | E_STRICT);

$testCases = array(
    'foo bar baz [url=http://example.com]anything[img]something[/url] foo bar baz',
    'foo bar baz [url=http://example.com]anythingsomething[/url] foo bar baz',
    'FOO BAR BAZ [URL=HTTP://EXAMPLE.COM]ANYTHING[IMG]SOMETHING[/URL] FOO BAR BAZ',
    'FOO BAR BAZ [URL=HTTP://EXAMPLE.COM]ANYTHINGSOMETHING[/URL] FOO BAR BAZ',
);

foreach ($testCases as $testCase) {
    $results[] = preg_replace("{\[url(?!.*?\[img\].*?).*?\[/url\]}is", '', $testCase);
};

print_r($results);

?>


I would try these lines of scripts

$subject = '[url=http]anything[imag]something[/url]';
$pattern = '@^\[url[^].]*\]{1}(.*)\[/url\]$@';
preg_match($pattern, $subject, $matches);
echo $matches[1]; //anything[imag]something

Is the result what you want?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜