Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 136,576 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,876 people online right now. Registration is fast and FREE... Join Now!




Preg_replace issues

 
Reply to this topicStart new topic

Preg_replace issues, Can't single out words!!!

maffelu
2 Oct, 2008 - 11:58 PM
Post #1

New D.I.C Head
*

Joined: 21 Aug, 2008
Posts: 37


My Contributions
Allrigh!

I have a highlighting function that is suppose to highligh certain C# keywords. But when it comes to words like 'if', 'is', 'do' and other small words, they can be found in other words, such as infinity, nifty and doh, so I want to be able to find ONLY 'if', 'is', 'in', etc.

Problem is, this won't work:
CODE

$sumstr = preg_replace('#\bin\b#', '<span class="blueBold">in</span', $sumstr);
$sumstr = preg_replace('#\bif\b#', '<span class="blueBold">if</span', $sumstr);
$sumstr = preg_replace('#\bis\b#', '<span class="blueBold">is</span', $sumstr);


It will work for the first one, but the rest won't work, and I'll lose alot of blankspaces in the text.

And if I do this:

CODE
$var = '#\bif\b#';
$sumstr = preg_replace($var, '<span class="blueBold">'.$var.'</span', $sumstr);


the 'if' in the text will be blue, but will also be 'bifb'. So it it gets blue and bold, but somehow the b's are added!


This post has been edited by maffelu: 3 Oct, 2008 - 02:05 AM
User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: Preg_replace Issues
3 Oct, 2008 - 06:13 AM
Post #2

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 3,995



Thanked: 16 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
using "/\bif\b/" as the regular expression appears to be working fine, of course if the word if is used in a comment it will also be altered.

There is a free php based syntax highlighter called geshi. I've used it in a few small scripts I've written it is easy to use and handles almost any language you can think of, which of course includes C#.

This post has been edited by William_Wilson: 3 Oct, 2008 - 06:18 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/3/08 12:14AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month