immanuelx2 Posted June 4, 2009 Share Posted June 4, 2009 Hey all. I've been trying to replace all occurrences of $var that are NOT found between sets of brackets [], but can't figure out how. Can anyone point me in the right direction? Much thanks in advance! These forums are very helpful Quote Link to comment https://forums.phpfreaks.com/topic/160951-solved-another-simple-preg_replace-problem/ Share on other sites More sharing options...
nrg_alpha Posted June 4, 2009 Share Posted June 4, 2009 You mean something along these lines? $str = 'Some text... $var Some more text... [$var] Even more text.. finally, $var and [$var]!'; $pattern = '/(^|\])[^[]+/'; $variable = '$var'; function replace($a){ global $variable; return str_replace($variable, '*whatever*', $a[0]); } $str = preg_replace_callback($pattern, 'replace', $str); echo $str; Output: Some text... *whatever* Some more text... [$var] Even more text.. finally, *whatever* and [$var]! Quote Link to comment https://forums.phpfreaks.com/topic/160951-solved-another-simple-preg_replace-problem/#findComment-849422 Share on other sites More sharing options...
immanuelx2 Posted June 4, 2009 Author Share Posted June 4, 2009 Great, thanks! Is there a way to do it with just a preg_replace function or does it require those extra steps? Quote Link to comment https://forums.phpfreaks.com/topic/160951-solved-another-simple-preg_replace-problem/#findComment-849496 Share on other sites More sharing options...
nrg_alpha Posted June 4, 2009 Share Posted June 4, 2009 Well, those 'extra steps' takes what preg finds (outside the [] characters), then to the replace function, uses str_replace to replace any $variable found. A regular preg_replace alone would not do all that. All you need to do is change a) the $variable that you want to see replaced, and b) $str to the string you want to check. Quote Link to comment https://forums.phpfreaks.com/topic/160951-solved-another-simple-preg_replace-problem/#findComment-849543 Share on other sites More sharing options...
immanuelx2 Posted June 4, 2009 Author Share Posted June 4, 2009 Well, those 'extra steps' takes what preg finds (outside the [] characters), then to the replace function, uses str_replace to replace any $variable found. A regular preg_replace alone would not do all that. All you need to do is change a) the $variable that you want to see replaced, and b) $str to the string you want to check. Hmm. Thanks for the reply... Here is my delimma though I am trying to get a search highlighted fields script going, and right now I have $article_short = preg_replace("/\b(".$_GET['search'].")\b/i", '<span style="background:#ffffab">\1</span>', $article['short']); that goes through and encloses all instances of $_GET['search'] with that span tag. However, the problem is that if there is a with the variable present, it will replace the URL with span tags and mess it up. So how can I do that? Your example used str_replace which I don't think will work for my situation. Quote Link to comment https://forums.phpfreaks.com/topic/160951-solved-another-simple-preg_replace-problem/#findComment-849619 Share on other sites More sharing options...
nrg_alpha Posted June 5, 2009 Share Posted June 5, 2009 Well I suppose we could exchange str_replace for a version of your preg_replace like so: Example: $article['short'] = 'Some text... Car Some more text... [Car] Even more text.. finally, Car and [Car]!'; $pattern = '#(^|\])[^[]+#'; $_GET['search'] = 'some'; // Obviously, you won't use this line, as you have your $_GET value.. I just use this as a test function replace($a){ return preg_replace('#\b('.$_GET['search'].')\b#i', '<span style="background:#ffffab">\1</span>', $a[0]); } $article_short = preg_replace_callback($pattern, 'replace', $article['short']); echo $article_short; Quote Link to comment https://forums.phpfreaks.com/topic/160951-solved-another-simple-preg_replace-problem/#findComment-849653 Share on other sites More sharing options...
immanuelx2 Posted June 5, 2009 Author Share Posted June 5, 2009 Well I suppose we could exchange str_replace for a version of your preg_replace like so: Example: $article['short'] = 'Some text... Car Some more text... [Car] Even more text.. finally, Car and [Car]!'; $pattern = '#(^|\])[^[]+#'; $_GET['search'] = 'some'; // Obviously, you won't use this line, as you have your $_GET value.. I just use this as a test function replace($a){ return preg_replace('#\b('.$_GET['search'].')\b#i', '<span style="background:#ffffab">\1</span>', $a[0]); } $article_short = preg_replace_callback($pattern, 'replace', $article['short']); echo $article_short; This worked Thanks a lot for helping with this complex problem. However, now I realize that it still return articles if a match is found in the URLS... so I have to figure out a way for MySQL to recognize where the URLS are and not include them in the LIKE '%search%' statement Quote Link to comment https://forums.phpfreaks.com/topic/160951-solved-another-simple-preg_replace-problem/#findComment-849683 Share on other sites More sharing options...
nrg_alpha Posted June 5, 2009 Share Posted June 5, 2009 You can post that issue in the MySQL forum and flag this thread as resolved. Quote Link to comment https://forums.phpfreaks.com/topic/160951-solved-another-simple-preg_replace-problem/#findComment-849724 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.