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
Link to comment
Share on other sites

[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]
Link to comment
Share on other sites

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?
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.