473,386 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Match case

MrMancunian
569 Expert 512MB
Hi,

I wrote a very simple piece of code that performs a search for a string. It finds whatever I search when I query the database like this:

Expand|Select|Wrap|Line Numbers
  1. $query = "SELECT * FROM blog WHERE subject LIKE '%$search%' OR message LIKE '%$search%'";
Next, I want to display the results and shade the search string. When a part of the result is exactly the same as the search string, it works fine. So Steven = Steven, but Steven <> steven. I use the following line of code to shade the search string:

Expand|Select|Wrap|Line Numbers
  1. $message = str_replace($search,"<font style=\"BACKGROUND-COLOR: yellow\">" . $search . "</font>",$message);
What should I do to make sure that the search string will match, even if there is a capital off?

Thanks,

Steven
Aug 25 '09 #1

✓ answered by Markus

And here is my first attempt: AccentedString.

Mark.

16 3199
ziycon
384 256MB
Try using strtolower(): http://ie.php.net/strtolower
Aug 25 '09 #2
Markus
6,050 Expert 4TB
Or the case-insensitive version: str_ireplace().

Mark.
Aug 25 '09 #3
MrMancunian
569 Expert 512MB
I've thought of that, but I'm not sure how to implement it...Any ideas?
Aug 25 '09 #4
Markus
6,050 Expert 4TB
@MrMancunian
Stick an 'i' before the 'replace' in 'str_replace'.

;)
Aug 25 '09 #5
MrMancunian
569 Expert 512MB
@Markus
Haha, we posted our replies at the same time, I responded to ziycon :-)

When I use str_ireplace, the text that matches my search string changes to that string. So if the search string is Steven and the text has steven or StEvEn in it, it will change to Steven.

Expand|Select|Wrap|Line Numbers
  1. $message = str_ireplace($search,"<font style=\"BACKGROUND-COLOR: yellow\">" . $search . "</font>",$message);
How do I change this?
Aug 25 '09 #6
Markus
6,050 Expert 4TB
@MrMancunian
Ah, then it gets a little more complicated. As far as I can tell, you'll need to use something like preg_replace() and regular expressions.

Expand|Select|Wrap|Line Numbers
  1. $text = "Mark mark mArK m ark";
  2.  
  3. $text = preg_replace('/(Mark)/i', '<font color="yellow">$1</font>', $text);
  4. print $text;
  5.  
Mark.

Edit: Tested - works.
Edit2: Dorm, you've got to type faster ;)
Aug 25 '09 #7
MrMancunian
569 Expert 512MB
@Markus
Ok, so $text is my $message. Why do you use ('/(Mark)/i'? What should I use instead?

Steven
Aug 25 '09 #8
Dormilich
8,658 Expert Mod 8TB
@MrMancunian
because the text contained that word as example. and '/(Mark)/i' is the required RegEx for this.

@MrMancunian
e.g.
Expand|Select|Wrap|Line Numbers
  1. '/(Steven)/i'
Aug 25 '09 #9
MrMancunian
569 Expert 512MB
Ok, the capitals work...Now, the special characters! :-)

Maxima <> Màxima...How do I include this as well?

Steven
Aug 25 '09 #10
Dormilich
8,658 Expert Mod 8TB
that’s abit tricky, you can use classes to catch that in the RegEx
Expand|Select|Wrap|Line Numbers
  1. "§M[aáà]xima§i"
though you might need something to construct the search pattern then
Aug 25 '09 #11
MrMancunian
569 Expert 512MB
Ouch, sounds like it's difficult. Can you explain how I use this for every possible character? Or can you point me to documentation which could be helpful?

Thanks,

Steven
Aug 26 '09 #12
Markus
6,050 Expert 4TB
Hmm, interesting.

My thoughts are you will have to 'normalize' the string, while making a note of which characters (and their position in the string) have been normalized. Then, once you have used your regular expression, de-normalize the string.

Sounds like a good job for a class.

Mark (goes to have a fiddle*!).

* With the programming - not whatever you were thinking ;)
Aug 26 '09 #13
Markus
6,050 Expert 4TB
And here is my first attempt: AccentedString.

Mark.
Aug 30 '09 #14
hsriat
1,654 Expert 1GB
Hi Steven,

Please do not use LIKE keyword for this purpose. Use MATCH-AGAINST for the search.

str_replace or str_ireplace will not solve your problem as php replacement is done after you have searched the database. It might give you correct results sometime, but not always.

You can have a look at an example similar to your case here.
Aug 31 '09 #15
MrMancunian
569 Expert 512MB
@Markus
Hi Mark,

Thanks for your efforts. As soon as I find the time for it, I'll take a look at it and see if I can implement it. I'll let you know!

Steven
Aug 31 '09 #16
MrMancunian
569 Expert 512MB
@hsriat
Thanks for your suggestion! I'll look into it as soon as possible :-)

Steven
Aug 31 '09 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Follower | last post by:
Hi, I am working on a function to return extracts from a text document with a specific phrase highlighted (i.e. display the context of the matched phrase). The requirements are: * Match...
6
by: Duane Morin | last post by:
I've inherited an XSL transform that I need to squeeze every last millisecond out of (since it's running several hundred thousand times). I've noticed that there are 26 match clauses in the file....
3
by: Andy Fish | last post by:
Hi, I'm trying to use <xsl:number> to generate a sequence number of all nodes that match a particular pattern, e.g: <xsl:template match="foo"> <xsl:copy> <xsl:attribute name="id"><xsl:number...
19
by: Tom Deco | last post by:
Hi, I'm trying to use a regular expression to match a string containing a # (basically i'm looking for #include ...) I don't seem to manage to write a regular expression that matches this. ...
3
by: Alexandre | last post by:
Hi! I receive this error in my webapp: Ambiguous match found. At line: Line 1: <%@ page language="C#" masterpagefile="~/memberscontents/master_interna.master" autoeventwireup="true"...
4
by: jmdaviault | last post by:
I want to do the equivalent of SELECT id from TABLE WHERE text='text' only fast solution I found is: SELECT id,text from TABLE WHERE MATCH(text) AGAINST('value' IN BOOLEAN MODE) HAVING...
12
by: Johnny Williams | last post by:
I'm struggling to create a regular expression for use with VB .Net which matches a person's name in a string of words. For example in "physicist Albert Einstein was born in Germany and" I want...
32
by: Licheng Fang | last post by:
Basically, the problem is this: 'do' Python's NFA regexp engine trys only the first option, and happily rests on that. There's another example: 'oneself' The Python regular expression...
3
by: neolempires2 | last post by:
hi..i'm new in sql progaming, i try to make make a query that in table field "match" return to "1" if no member record in another table and return to "0" if there is any record member : ex...
19
by: konrad Krupa | last post by:
I'm not expert in Pattern Matching and it would take me a while to come up with the syntax for what I'm trying to do. I hope there are some experts that can help me. I'm trying to match...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.