yaba Posted November 16, 2006 Share Posted November 16, 2006 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 https://forums.phpfreaks.com/topic/27525-match-text-not-included-in-other-specific-text/ Share on other sites More sharing options...
effigy Posted November 17, 2006 Share Posted November 17, 2006 Since it's tag-based (and the number could grow in the future), you'll need to split the line by tags, then check each result for the "blah." Link to comment https://forums.phpfreaks.com/topic/27525-match-text-not-included-in-other-specific-text/#findComment-126154 Share on other sites More sharing options...
yaba Posted November 17, 2006 Author Share Posted November 17, 2006 I only need to check for two tags: url and img, so some part of the regex could be "\[(img|url)(=.+?)?\]" (I think)I'm not sure what exactly you meant there? Could you post an example?Thanks for replying :D Link to comment https://forums.phpfreaks.com/topic/27525-match-text-not-included-in-other-specific-text/#findComment-126170 Share on other sites More sharing options...
effigy Posted November 17, 2006 Share Posted November 17, 2006 [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 OKArray( [0] => [url=http://aasdf blah]asdfgsdfg[/url])[url=http://aasdf blah]asdfgsdfg[/url] -- Not OKArray( [0] => [url=http://asdf]asdf blah asdf[/url])[url=http://asdf]asdf blah asdf[/url] -- Not OKArray( [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 https://forums.phpfreaks.com/topic/27525-match-text-not-included-in-other-specific-text/#findComment-126188 Share on other sites More sharing options...
yaba Posted November 18, 2006 Author Share Posted November 18, 2006 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 https://forums.phpfreaks.com/topic/27525-match-text-not-included-in-other-specific-text/#findComment-126471 Share on other sites More sharing options...
yaba Posted November 18, 2006 Author Share Posted November 18, 2006 May I rephrase my request:How can I get all instances of "blah" that do NOT belong in a URL, i.e. something starting with either http:// or www.Thanks again :) Link to comment https://forums.phpfreaks.com/topic/27525-match-text-not-included-in-other-specific-text/#findComment-126488 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.