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: - $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: - $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
16 3199
I've thought of that, but I'm not sure how to implement it...Any ideas?
@MrMancunian
Stick an 'i' before the 'replace' in 'str_replace'.
;)
@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. - $message = str_ireplace($search,"<font style=\"BACKGROUND-COLOR: yellow\">" . $search . "</font>",$message);
How do I change this?
@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. -
$text = "Mark mark mArK m ark";
-
-
$text = preg_replace('/(Mark)/i', '<font color="yellow">$1</font>', $text);
-
print $text;
-
Mark.
Edit: Tested - works.
Edit2: Dorm, you've got to type faster ;)
@Markus
Ok, so $text is my $message. Why do you use ('/(Mark)/i'? What should I use instead?
Steven
@MrMancunian
because the text contained that word as example. and '/(Mark)/i' is the required RegEx for this. @MrMancunian
e.g.
Ok, the capitals work...Now, the special characters! :-)
Maxima <> Màxima...How do I include this as well?
Steven
that’s abit tricky, you can use classes to catch that in the RegEx
though you might need something to construct the search pattern then
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
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 ;)
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.
@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
@hsriat
Thanks for your suggestion! I'll look into it as soon as possible :-)
Steven
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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....
|
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...
|
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.
...
|
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"...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
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
|
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...
|
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,...
|
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,...
|
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...
| |