tags:

views:

95

answers:

2

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

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

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

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

can anyone help? im bad with regular expressions, or there is easier way?

A: 

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?

kho
not exactly but will be usefull also, so first thing it wont remove (match) tags ([url ones) also. So it remove only what is inside tags, but it should remove whole thing if there is not [img] tag inside.
Edwardo
A: 

How about this:

<?php

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

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

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

print_r($results);

?>
mr. w
almost perfect :), it should remove when [img] is not inside, and now it remove when it is inside. Can this be easily fixed?
Edwardo
Sorry, guess I misread your comment above. I've modified the expression to pull those links without the IMG tag in them.
mr. w
working perfect :) thx so much
Edwardo