Re: highlighting searchterms as bold text

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



C.R.Vegelin wrote:
----- Original Message ----- From: "Puiu Hrenciuc" <hpuiu@xxxxxxxxx>
To: <php-general@xxxxxxxxxxxxx>
Sent: Thursday, September 20, 2007 12:56 PM
Subject: Re:  highlighting searchterms as bold text


Sorry, I didn't read d) right (NOT Methyl),
here is teh new regex :

/(\b#SearchTermHere#(\w+)?)/i

The code sample is the same, replace regex, of course .

Output should be :

<b>ethyl</b> Lorem <b>Ethyl</b> ipsum MeThYl dolor <b>Ethylene</b> sit

Cheers


Puiu Hrenciuc wrote:
Hi,

Here's what you need:

RegEx:

/((\w+)?#SearchTermHere#(\w+)?)/i

Of course, replace your #SearchTermHere# with what you need,
i.e. /((\w+)?ethyl(\w+)?)/i

Code sample:

$_textToHighlight='ethyl Lorem Ethyl ipsum MeThYl dolor Ethylene sit';
$_search='/((\w+)?ethyl(\w+)?)/i';
$_replace='<b>\\1</b>';
$_highlightedText=preg_replace($_search,$_replace,$_textToHighlight);

This should output:

<b>ethyl</b> Lorem <b>Ethyl</b> ipsum <b>MeThYl</b> dolor <b>Ethylene</b> sit

Hope it helps,
PuYa

C.R.Vegelin wrote:

----- Original Message ----- From: "Deniz Dizman (BST UGB)" <DENIZ.DIZMAN@xxxxxxxxxxxxx> To: "C.R.Vegelin" <cr.vegelin@xxxxxxxxx>; "php-general@xxxxxxxxxxxx" <php-general@xxxxxxxxxxxxx>
Sent: Thursday, September 20, 2007 11:29 AM
Subject: RE:  highlighting searchterms as bold text


thats odd because the regex passes when I test it with this online tool: http://samuelfullman.com/team/php/tools/regular_expression_tester_p2.php

--
dd

-----Original Message-----
From: C.R.Vegelin [mailto:cr.vegelin@xxxxxxxxx]
Sent: Thursday, September 20, 2007 1:59 PM
To: Deniz Dizman (BST UGB); php-general@xxxxxxxxxxxx
Subject: Re:  highlighting searchterms as bold text


----- Original Message -----
From: "Deniz Dizman (BST UGB)" <DENIZ.DIZMAN@xxxxxxxxxxxxx>
To: "C.R.Vegelin" <cr.vegelin@xxxxxxxxx>; "php-general@xxxxxxxxxxxx"
<php-general@xxxxxxxxxxxxx>
Sent: Thursday, September 20, 2007 10:38 AM
Subject: RE:  highlighting searchterms as bold text


try /^ethyl/i
what you want is ignore case and pattern begins with

--
dd

> -----Original Message-----
> From: C.R.Vegelin [mailto:cr.vegelin@xxxxxxxxx]
> Sent: Thursday, September 20, 2007 1:31 PM
> To: php-general@xxxxxxxxxxxx
> Subject:  highlighting searchterms as bold text
>
> Hi everyone,
>
> I want to highlight (bold) searchterms in text.
> For example, for all words starting with "ethyl":
> a) replace "ethyl" by "<b>ethyl</b>"
> b) replace "Ethyl" by "<b>Ethyl</b>"
> c) replace "ethylene" by "<b>ethylene</b>"
> d) but not "methyl" by "<b>methyl</b>"
>
> Now I use:
> $patterns[0] = "/ethyl/";
> $replacements[0] = "<b>ethyl</b>";
> $text = preg_replace($patterns, $replacements, $text);
>
> This works for a) and c), but not for b) and d).
> Any idea how to do this ?
>
> TIA, Cor
>

Thanks Deniz,

I tried pattern /^ethyl/i but this does not highlight anything.
I also tried pattern /ethyl/i and this highlights all, but
also methyl ...
Any suggestion ?

Cor

Hi Deniz,

I tried the referred regex tool  as well,
and I get Bad Result for /^ethyl/i
and Good Result for /ethyl/i

Cor

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Thanks Puiu,

I tested your regex with the code below.
However, it didn't work.
Maybe I'm missing something ...
To test it, change the $pattern setting.

<?php
 $term = "ethyl";
 $text = "Ethylene, ethyl and methyl are different things.";
 $pattern = "/(\b#" . $term . "#(\w+)?)/i";
 $replacement = "<b>" . $term . "</b>";
 // or: $replacement = "<b>\\1</b>";
 echo "before: " . $text, "<br />";
 $text = preg_replace($pattern, $replacement, $text);
 echo "after: " . $text, "<br />";

echo "wanted:<br /><b>Ethyl</b>ene, <b>ethyl</b> and methyl are different things.", "<br />"; echo "or:<br /><b>ethyl</b>ene, <b>ethyl</b> and methyl are different things.", "<br />";
?>

Regards, Cor

you forgot to also remove '#' and enclose in single quotes,
otherwise the '\' character has another meaning :)
Also, the replacement should be exactly as I gave it, with \\1,
it means 'put here the string that matched first brackets pair '


So these two lines should look like:


$pattern = '/(\b' . $term . '(\w+)?)/i';
$replacement = '<b>\\1</b>';

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux