Jump to content

match text NOT included in other specific text


yaba

Recommended Posts

Hi everyone,

could you please tell me how can I get all instances of "blah" that are NOT included in things like

[code]
[img]...[/img]
and
[url=http://blah.com]..[/url]
[/code]

So, the following should not match:

[code]
[img]asdfa asdf asd blah asdg [/img]
[url=aasdf blah]asdfgsdfg[/url]
[url=asdf]asdf blah asdf[/url]
[/code]

while this should match once:
[code]
[img]asdfa blah asdf[/img] asdfas sdf blah asgf [url=blah]blah[/url]
[/code]

Thanks! :D
[code]
<pre>
<?php

$tests = array(
'[img]asdfa asdf asd blah asdg [/img]',
'[url=aasdf blah]asdfgsdfg[/url]',
'[url=asdf]asdf blah asdf[/url]',
'[img]asdfa blah asdf[/img] asdfas sdf blah asgf [url=blah]blah[/url]'
);

foreach ($tests as $test) {
### Separate the tags from the text.
$pieces = preg_split(
'%(\[.+?\].*?\[/.+?\])%',
$test,
-1,
PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
);
print_r($pieces);
### Check each piece...
foreach ($pieces as $piece) {
echo "$piece -- ";
### ...if it's not a tag, it can have "blah"
if (substr($piece, 0, 1) != '[') {
echo 'OK';
}
else {
echo strstr($piece, 'blah') ? 'Not OK' : 'OK' ;
}
echo '<br>';
}
echo '<br>';
}

?>
</pre>
[/code]

[b]Update:[/b] These are still being interpreted as bbcodes even though I've checked not too; hopefully you get the idea...

[tt]
Array
(
    [0] => [img]http://asdfa asdf asd blah asdg[/img]
)
[img]http://asdfa asdf asd blah asdg[/img] -- Not OK

Array
(
    [0] => [url=http://aasdf blah]asdfgsdfg[/url]
)
[url=http://aasdf blah]asdfgsdfg[/url] -- Not OK

Array
(
    [0] => [url=http://asdf]asdf blah asdf[/url]
)
[url=http://asdf]asdf blah asdf[/url] -- Not OK

Array
(
    [0] => [img]http://asdfa blah asdf[/img]
    [1] =>  asdfas sdf blah asgf
    [2] => [url=http://blah]blah[/url]
)
[img]http://asdfa blah asdf[/img] -- Not OK
asdfas sdf blah asgf  -- OK
[url=http://blah]blah[/url] -- Not OK
[/tt]
I was hoping something a lot simpler and less CPU consuming would exist. For the [img] tag, something like:

[code=php:0]
$blah= ".+?blah.+?";
$text = preg_replace( "#[^(\[img\])]".$blah."[^(\[/img\])]#i", "replaced", $text);
[/code]

Any feedback on something like this?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.