Sharif T. Karim wrote:
Michael Austin, being the foo Michael Austin is, wrote:
Sharif T. Karim wrote:
I am trying to do the following with my search script that looks for
records in a mysql table. The following is an example of what I am
trying to do.
Text being searched:
The brown fox jumped over the green fence then jumped into the web
monitor. It was hurt so it jumped backwards and fell on its!
Return all records and use something like:
database query:
select field from table where field like "% web %";
// in this case the space before will get you web and not webster
etc...
$pattern = "web";
$string = [string returned from mysql]
$replacement = "<b>*$pattern*</b>"
echo preg_replace($pattern, $replacement, $string);
Thanks, but that only replaces words. I am trying to get the result
($string) in this case, to start showing the part where the first occurence
of $pattern is.
I can't always give you the exact syntax. what I gave you was close, but
you need to be able to take what you are given and test/modify it to
suit your needs...
$search-from-form = $_POST['inputfield'];
$pattern = "-$search-from-form-";
//sear pattern needs non-alphanumberic delimiters
$string = "The web is a scary place to be";
//string to be searched
$rpat = "$search-from-form";
//now replace the string with the search "word"
$replacement = "<b>*$rpat*</b>";
echo "STRING = $string<br>\n";
echo preg_replace($pattern, $replacement, $string);
results in
The *web* is a scary place to be
(only in your browser *web* is bold).
This took a whole 10 minutes to prepare... is this for your job or just
playing/learning? Did you happen to read the docs on the previous
example? If not, why not???
Michael Austin.
Consultant - Available.
Donations welcomed.
Http://www.firstdbasource.com/donations.html
:)