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 Quote Link to comment 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." Quote Link to comment 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 Quote Link to comment 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] Quote Link to comment 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? Quote Link to comment 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 :) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.