PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > php.general > highlighting searchterms as bold text
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
highlighting searchterms as bold text

Réponse
 
LinkBack Outils de la discussion
Vieux 20/09/2007, 11h30   #1
C.R.Vegelin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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
  Réponse avec citation
Vieux 20/09/2007, 11h33   #2
Edward Kay
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut RE: [PHP] 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 ?
>


Yes, the i pattern modifier makes the search case-insensitive, which is what
you want.

The carat ^ means start of string, so would only word if the pattern was at
the beginning of the line - not what you want.

Try something like this:

/(\s|>)ethyl(\s|<)/i

The \s means whitespace so (\s|>) means only match if ethyl is preceded by
whitespace or >, i.e. a closing tag.

Alternatively, have a look at http://suda.co.uk/projects/SEHL/.

Edward

  Réponse avec citation
Vieux 20/09/2007, 11h58   #3
C.R.Vegelin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] highlighting searchterms as bold text


----- Original Message -----
From: "Deniz Dizman (BST UGB)" <DENIZ.DIZMAN@fortis.com.tr>
To: "C.R.Vegelin" <cr.vegelin@hetnet.nl>; "php-general@lists.php.nt"
<php-general@lists.php.net>
Sent: Thursday, September 20, 2007 10:38 AM
Subject: RE: [php] 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@hetnet.nl]
> Sent: Thursday, September 20, 2007 1:31 PM
> To: php-general@lists.php.nt
> Subject: [php] 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
  Réponse avec citation
Vieux 20/09/2007, 12h45   #4
Puiu Hrenciuc
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] highlighting searchterms as bold text

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 s,
PuYa

C.R.Vegelin wrote:
>
> ----- Original Message ----- From: "Deniz Dizman (BST UGB)"
> <DENIZ.DIZMAN@fortis.com.tr>
> To: "C.R.Vegelin" <cr.vegelin@hetnet.nl>; "php-general@lists.php.nt"
> <php-general@lists.php.net>
> Sent: Thursday, September 20, 2007 11:29 AM
> Subject: RE: [php] 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@hetnet.nl]
>> Sent: Thursday, September 20, 2007 1:59 PM
>> To: Deniz Dizman (BST UGB); php-general@lists.php.nt
>> Subject: Re: [php] highlighting searchterms as bold text
>>
>>
>> ----- Original Message -----
>> From: "Deniz Dizman (BST UGB)" <DENIZ.DIZMAN@fortis.com.tr>
>> To: "C.R.Vegelin" <cr.vegelin@hetnet.nl>; "php-general@lists.php.nt"
>> <php-general@lists.php.net>
>> Sent: Thursday, September 20, 2007 10:38 AM
>> Subject: RE: [php] 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@hetnet.nl]
>> > Sent: Thursday, September 20, 2007 1:31 PM
>> > To: php-general@lists.php.nt
>> > Subject: [php] 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

  Réponse avec citation
Vieux 20/09/2007, 12h56   #5
Puiu Hrenciuc
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] 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 s,
> PuYa
>
> C.R.Vegelin wrote:
>>
>> ----- Original Message ----- From: "Deniz Dizman (BST UGB)"
>> <DENIZ.DIZMAN@fortis.com.tr>
>> To: "C.R.Vegelin" <cr.vegelin@hetnet.nl>; "php-general@lists.php.nt"
>> <php-general@lists.php.net>
>> Sent: Thursday, September 20, 2007 11:29 AM
>> Subject: RE: [php] 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@hetnet.nl]
>>> Sent: Thursday, September 20, 2007 1:59 PM
>>> To: Deniz Dizman (BST UGB); php-general@lists.php.nt
>>> Subject: Re: [php] highlighting searchterms as bold text
>>>
>>>
>>> ----- Original Message -----
>>> From: "Deniz Dizman (BST UGB)" <DENIZ.DIZMAN@fortis.com.tr>
>>> To: "C.R.Vegelin" <cr.vegelin@hetnet.nl>; "php-general@lists.php.nt"
>>> <php-general@lists.php.net>
>>> Sent: Thursday, September 20, 2007 10:38 AM
>>> Subject: RE: [php] 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@hetnet.nl]
>>> > Sent: Thursday, September 20, 2007 1:31 PM
>>> > To: php-general@lists.php.nt
>>> > Subject: [php] 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

  Réponse avec citation
Vieux 20/09/2007, 13h06   #6
Robert Cummings
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] highlighting searchterms as bold text

On Thu, 2007-09-20 at 13:09 +0100, C.R.Vegelin wrote:
> ----- Original Message -----
> From: "Deniz Dizman (BST UGB)" <DENIZ.DIZMAN@fortis.com.tr>
> To: "C.R.Vegelin" <cr.vegelin@hetnet.nl>; "php-general@lists.php.nt"
> <php-general@lists.php.net>
> Sent: Thursday, September 20, 2007 11:29 AM
> Subject: RE: [php] 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


That makes it syntactically correct... but it tells you nothing about
logic.

> > -----Original Message-----
> > From: C.R.Vegelin [mailto:cr.vegelin@hetnet.nl]
> > Sent: Thursday, September 20, 2007 1:59 PM
> > To: Deniz Dizman (BST UGB); php-general@lists.php.nt
> > Subject: Re: [php] highlighting searchterms as bold text
> >
> >
> > ----- Original Message -----
> > From: "Deniz Dizman (BST UGB)" <DENIZ.DIZMAN@fortis.com.tr>
> > To: "C.R.Vegelin" <cr.vegelin@hetnet.nl>; "php-general@lists.php.nt"
> > <php-general@lists.php.net>
> > Sent: Thursday, September 20, 2007 10:38 AM
> > Subject: RE: [php] 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@hetnet.nl]
> > > Sent: Thursday, September 20, 2007 1:31 PM
> > > To: php-general@lists.php.nt
> > > Subject: [php] 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


That's because the ^ character matches the beginning of a line and so it
would only match ethyl when it is the first word. As some others pointed
out, you really want to match the word boundary so that you match ethyl
when it is the beginning of a word.

Cheers,
Rob.
--
.................................................. ..........
SwarmBuy.com - http://www.swarmbuy.com

Leveraging the buying power of the masses!
.................................................. ..........
  Réponse avec citation
Vieux 20/09/2007, 13h09   #7
C.R.Vegelin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] highlighting searchterms as bold text


----- Original Message -----
From: "Deniz Dizman (BST UGB)" <DENIZ.DIZMAN@fortis.com.tr>
To: "C.R.Vegelin" <cr.vegelin@hetnet.nl>; "php-general@lists.php.nt"
<php-general@lists.php.net>
Sent: Thursday, September 20, 2007 11:29 AM
Subject: RE: [php] 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@hetnet.nl]
> Sent: Thursday, September 20, 2007 1:59 PM
> To: Deniz Dizman (BST UGB); php-general@lists.php.nt
> Subject: Re: [php] highlighting searchterms as bold text
>
>
> ----- Original Message -----
> From: "Deniz Dizman (BST UGB)" <DENIZ.DIZMAN@fortis.com.tr>
> To: "C.R.Vegelin" <cr.vegelin@hetnet.nl>; "php-general@lists.php.nt"
> <php-general@lists.php.net>
> Sent: Thursday, September 20, 2007 10:38 AM
> Subject: RE: [php] 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@hetnet.nl]
> > Sent: Thursday, September 20, 2007 1:31 PM
> > To: php-general@lists.php.nt
> > Subject: [php] 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
  Réponse avec citation
Vieux 20/09/2007, 14h09   #8
C.R.Vegelin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] highlighting searchterms as bold text

----- Original Message -----
From: "Deniz Dizman (BST UGB)" <DENIZ.DIZMAN@fortis.com.tr>
To: "C.R.Vegelin" <cr.vegelin@hetnet.nl>; "php-general@lists.php.nt"
<php-general@lists.php.net>
Sent: Thursday, September 20, 2007 12:25 PM
Subject: RE: [php] highlighting searchterms as bold text


you probably have some characters before/after the phrase that prevents
the match.
This should do it: (i hope)
/(\S|\s)*\bethyl\b(\S|\s)*/i

--
dd

> -----Original Message-----
> From: C.R.Vegelin [mailto:cr.vegelin@hetnet.nl]
> Sent: Thursday, September 20, 2007 3:09 PM
> To: Deniz Dizman (BST UGB); php-general@lists.php.nt
> Subject: Re: [php] highlighting searchterms as bold text
>
>
> ----- Original Message -----
> From: "Deniz Dizman (BST UGB)" <DENIZ.DIZMAN@fortis.com.tr>
> To: "C.R.Vegelin" <cr.vegelin@hetnet.nl>; "php-general@lists.php.nt"
> <php-general@lists.php.net>
> Sent: Thursday, September 20, 2007 11:29 AM
> Subject: RE: [php] 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_tes
> ter_p2.php
>
> --
> dd
>
> > -----Original Message-----
> > From: C.R.Vegelin [mailto:cr.vegelin@hetnet.nl]
> > Sent: Thursday, September 20, 2007 1:59 PM
> > To: Deniz Dizman (BST UGB); php-general@lists.php.nt
> > Subject: Re: [php] highlighting searchterms as bold text
> >
> >
> > ----- Original Message -----
> > From: "Deniz Dizman (BST UGB)" <DENIZ.DIZMAN@fortis.com.tr>
> > To: "C.R.Vegelin" <cr.vegelin@hetnet.nl>; "php-general@lists.php.nt"
> > <php-general@lists.php.net>
> > Sent: Thursday, September 20, 2007 10:38 AM
> > Subject: RE: [php] 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@hetnet.nl]
> > > Sent: Thursday, September 20, 2007 1:31 PM
> > > To: php-general@lists.php.nt
> > > Subject: [php] 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
>
>

Hi Deniz, Edward, Mike,

Thanks for your suggestions.

Deniz,

pattern /(\S|\s)*\bethyl\b(\S|\s)*/i
makes from any line containing ethyl, such as
Undenatured ethyl alcohol
lines containing only: >ethyl

Edward,

pattern /(\s|>)ethyl(\s|<)/i
makes from: Undenatured ethyl alcohol
Undenaturedethylalcohol (stripping spaces).
I will take a look at http://suda.co.uk/projects/SEHL/.

Mike,

the \W character
makes from Undenatured ethyl alcohol
Undenaturedethylalcohol (stripping spaces).

The \b character makes:
Undenatured ethyl alcohol (okay)
the word methyl is unchanged (okay)
but ethylene keeps also unchanged ...

Thanks anyway !
Cor
  Réponse avec citation
Vieux 20/09/2007, 14h09   #9
Puiu Hrenciuc
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] highlighting searchterms as bold text

C.R.Vegelin wrote:
> ----- Original Message ----- From: "Puiu Hrenciuc" <hpuiu@xentra.ro>
> To: <php-general@lists.php.net>
> Sent: Thursday, September 20, 2007 12:56 PM
> Subject: Re: [php] 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 s,
>>> PuYa
>>>
>>> C.R.Vegelin wrote:
>>>>
>>>> ----- Original Message ----- From: "Deniz Dizman (BST UGB)"
>>>> <DENIZ.DIZMAN@fortis.com.tr>
>>>> To: "C.R.Vegelin" <cr.vegelin@hetnet.nl>; "php-general@lists.php.nt"
>>>> <php-general@lists.php.net>
>>>> Sent: Thursday, September 20, 2007 11:29 AM
>>>> Subject: RE: [php] 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@hetnet.nl]
>>>>> Sent: Thursday, September 20, 2007 1:59 PM
>>>>> To: Deniz Dizman (BST UGB); php-general@lists.php.nt
>>>>> Subject: Re: [php] highlighting searchterms as bold text
>>>>>
>>>>>
>>>>> ----- Original Message -----
>>>>> From: "Deniz Dizman (BST UGB)" <DENIZ.DIZMAN@fortis.com.tr>
>>>>> To: "C.R.Vegelin" <cr.vegelin@hetnet.nl>; "php-general@lists.php.nt"
>>>>> <php-general@lists.php.net>
>>>>> Sent: Thursday, September 20, 2007 10:38 AM
>>>>> Subject: RE: [php] 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@hetnet.nl]
>>>>> > Sent: Thursday, September 20, 2007 1:31 PM
>>>>> > To: php-general@lists.php.nt
>>>>> > Subject: [php] 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>';
  Réponse avec citation
Vieux 20/09/2007, 14h42   #10
C.R.Vegelin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] highlighting searchterms as bold text

----- Original Message -----
From: "Puiu Hrenciuc" <hpuiu@xentra.ro>
To: <php-general@lists.php.net>
Sent: Thursday, September 20, 2007 12:56 PM
Subject: Re: [php] 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 s,
>> PuYa
>>
>> C.R.Vegelin wrote:
>>>
>>> ----- Original Message ----- From: "Deniz Dizman (BST UGB)"
>>> <DENIZ.DIZMAN@fortis.com.tr>
>>> To: "C.R.Vegelin" <cr.vegelin@hetnet.nl>; "php-general@lists.php.nt"
>>> <php-general@lists.php.net>
>>> Sent: Thursday, September 20, 2007 11:29 AM
>>> Subject: RE: [php] 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@hetnet.nl]
>>>> Sent: Thursday, September 20, 2007 1:59 PM
>>>> To: Deniz Dizman (BST UGB); php-general@lists.php.nt
>>>> Subject: Re: [php] highlighting searchterms as bold text
>>>>
>>>>
>>>> ----- Original Message -----
>>>> From: "Deniz Dizman (BST UGB)" <DENIZ.DIZMAN@fortis.com.tr>
>>>> To: "C.R.Vegelin" <cr.vegelin@hetnet.nl>; "php-general@lists.php.nt"
>>>> <php-general@lists.php.net>
>>>> Sent: Thursday, September 20, 2007 10:38 AM
>>>> Subject: RE: [php] 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@hetnet.nl]
>>>> > Sent: Thursday, September 20, 2007 1:31 PM
>>>> > To: php-general@lists.php.nt
>>>> > Subject: [php] 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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 22h58.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,39338 seconds with 18 queries