Jump to content
php.lv forumi

Word Highlighting In Search Text


dodge

Recommended Posts

Moina php pusee:)

 

Lieta sekojosha... Ir nepiecieshams hailaitot vaardus kuri atrodas htmlaa. Protams nedriikst hailaitot tos vaardus, kuri ir ieksh html tagiem. Es te uzkreceleeju vienu patternu, bet nu nerullee kaa gribeetos...

 

Reku snippets no koda...

<code><font color="#000000">

<font color="#0000CC"><? 

</font><font color="#006600">while (list(, </font><font color="#0000CC">$value</font><font color="#006600">) = </font><font color="#0000CC">each </font><font color="#006600">(</font><font color="#0000CC">$words</font><font color="#006600">)) {
   </font><font color="#0000CC">$out </font><font color="#006600">= </font><font color="#0000CC">[url=http://lv.php.net/preg_replace]preg_replace[/url]</font><font color="#006600">(</font><font color="#CC0000">"/(?(?!(<[^>]*" </font><font color="#006600">. </font><font color="#0000CC">[url=http://lv.php.net/quotemeta]quotemeta[/url]</font><font color="#006600">(</font><font color="#0000CC">$value</font><font color="#006600">) . </font><font color="#CC0000">"[^>]*>.*<[^>]*>))b(" </font><font color="#006600">. </font><font color="#0000CC">[url=http://lv.php.net/quotemeta]quotemeta[/url]</font><font color="#006600">(</font><font color="#0000CC">$value</font><font color="#006600">) . </font><font color="#CC0000">")B)/iUs"</font><font color="#006600">, </font><font color="#CC0000">"<b style="</font><font color="#0000CC">color</font><font color="#006600">:</font><font color="#0000CC">black</font><font color="#006600">;</font><font color="#0000CC">background</font><font color="#006600">-</font><font color="#0000CC">color</font><font color="#006600">:</font><font color="#FF9900">#ffff66"></b>", $out);
</font><font color="#006600">}

</font><font color="#0000CC">?>
</font>
</font>
</code>

 

echo $out;

 

Kjip ar domu $out glabaajaas outputs un $words masiivs ar vaardiem kurus hailaitot...

Any ideas!!!

 

dodgis:)

Link to comment
Share on other sites

foreach($word as $one)

{

while(preg_match('/b'.$one.'b/imU',$out,$match))

{

$out=str_replace($match[1],'<b>'.$match[1].'</b>',$out);

}

}

 

 

raxtu no galvas+droši vien nostripsleshosies dazhi slashi, tāpēc 90% garantēts ka šitais koda klunķis tev ieciklēsies uz bezgalību ;-)

Link to comment
Share on other sites

Esi paarliecinaac, ka vish nerepleisos arii piemeeram sekojoshi...

Mekleejamais vaards "kasiko"

Attieciigi html gabals

 

<a href="kasiko">kasiko</a>

 

Man liekas ka peec taa patterna rezultaats buus sekojoshsss

<a href="<b>kasiko</b>"><b>kasiko</b></a>

 

Kas neir kaa vaig:))

 

dodge

Link to comment
Share on other sites

  • 1 month later...

Te buus source no taa faila

 

<code><font color="#000000">

<font color="#0000CC"><?php

</font><font color="#FF9900">/*

2 December, 2002

This is Google Hilite 0.3

by Dean Allen
http://textism.com 

When applied to a page arrived at via Google, this function will 
highlight the terms in the Google search wherever they appear. 
This is done through a CSS class.

How to use:

1. Save this file as 'google_hilite.php' somewhere on your server.

2. Add a style to your CSS:

.hilite { background-color: #FF9; }

(By all means change the colour, but the class must be called .hilite)

3. Place this at the top of your pages or template:

<?php 
$dr = $_SERVER[DOCUMENT_ROOT];
include_once($dr.'/PATH/TO/google_hilite.php');
ob_start("google_hilite"); 
?>

Place the following wherever you'd like to welcome the google visitor:

<? google_welcome(); ?>

*/

</font><font color="#0000CC">?></font> 
<font color="#0000CC"><?php


</font><font color="#006600">function </font><font color="#0000CC">google_hilite</font><font color="#006600">(</font><font color="#0000CC">$text</font><font color="#006600">) {
 </font><font color="#0000CC">$ref </font><font color="#006600">= </font><font color="#0000CC">[url=http://lv.php.net/urldecode]urldecode[/url]</font><font color="#006600">(</font><font color="#0000CC">$_SERVER</font><font color="#006600">[</font><font color="#0000CC">HTTP_REFERER</font><font color="#006600">]);

	</font><font color="#FF9900"># let's see if the referrer is google
  </font><font color="#006600">if (</font><font color="#0000CC">[url=http://lv.php.net/preg_match]preg_match[/url]</font><font color="#006600">(</font><font color="#CC0000">'/^http://w?w?w?.?google.*/i'</font><font color="#006600">,</font><font color="#0000CC">$ref</font><font color="#006600">)) {

	</font><font color="#FF9900"># if so, tweezer out the search query
   </font><font color="#0000CC">$query </font><font color="#006600">= </font><font color="#0000CC">[url=http://lv.php.net/preg_replace]preg_replace[/url]</font><font color="#006600">(</font><font color="#CC0000">'/^.*q=([^&]+)&?.*$/i'</font><font color="#006600">,</font><font color="#CC0000">'$1'</font><font color="#006600">,</font><font color="#0000CC">$ref</font><font color="#006600">);

	</font><font color="#FF9900"># scrub away nasty quote marks
   </font><font color="#0000CC">$query </font><font color="#006600">= </font><font color="#0000CC">[url=http://lv.php.net/preg_replace]preg_replace[/url]</font><font color="#006600">(</font><font color="#CC0000">'/'</font><font color="#006600">|</font><font color="#CC0000">"/','',$query);

	# chop the search terms into an array
   $query_array = preg_split ("</font><font color="#006600">/[</font><font color="#0000CC">s</font><font color="#006600">,+.]+/</font><font color="#CC0000">",$query);

	# loop through the search terms
   foreach($query_array as $b)</font><font color="#006600">{</font><font color="#CC0000">

	if (!preg_match('/<.+>/',$text)) </font><font color="#006600">{</font><font color="#CC0000">

	# If there are no tags in the text, we'll just do a simple search and replace
	 $text = preg_replace('/(b'.$b.'b)/i','<span class="</font><font color="#0000CC">hilite</font><font color="#CC0000">"></font><font color="#006600">$</font><font color="#CC0000">1</span>',$text);  

	</font><font color="#006600">}</font><font color="#CC0000"> else </font><font color="#006600">{</font><font color="#CC0000">

	# If there are tags, we need to stay outside them
	 $text = preg_replace('/(?<=>)(</font><font color="#006600">[</font><font color="#CC0000">^<</font><font color="#006600">]</font><font color="#CC0000">+)?(b'.$b.'b)/i','</font><font color="#006600">$</font><font color="#CC0000">1<span class="</font><font color="#0000CC">hilite</font><font color="#CC0000">"></font><font color="#006600">$</font><font color="#CC0000">2</span>',$text);

	</font><font color="#006600">}</font><font color="#CC0000">
   </font><font color="#006600">}</font><font color="#CC0000">
  </font><font color="#006600">}</font><font color="#CC0000">

  return $text;
 </font><font color="#006600">}</font><font color="#CC0000">



# Call this function to inform the Google visitor of your intentions to highlight.

function google_welcome() </font><font color="#006600">{</font><font color="#CC0000">
 $ref = urldecode($_SERVER</font><font color="#006600">[</font><font color="#CC0000">HTTP_REFERER</font><font color="#006600">]</font><font color="#CC0000">);
 if (preg_match('/^http://w?w?w?.?google.*/i',$ref)) </font><font color="#006600">{</font><font color="#CC0000">
  $query = preg_replace('/^.*q=(</font><font color="#006600">[</font><font color="#CC0000">^&</font><font color="#006600">]</font><font color="#CC0000">+)&?.*</font><font color="#006600">$</font><font color="#CC0000">/i','</font><font color="#006600">$</font><font color="#CC0000">1',$ref);
 if (!empty($query)) </font><font color="#006600">{</font><font color="#CC0000"> 
  echo '<p><small>',

  # Change this if you like:
  '</font><font color="#006600">[</font><font color="#CC0000">Google search terms are highlighted</font><font color="#006600">]</font><font color="#CC0000">.',

  '</small></p>'; 
 </font><font color="#006600">}</font><font color="#CC0000">
</font><font color="#006600">}</font><font color="#CC0000">
</font><font color="#006600">}</font><font color="#CC0000">


?>
</font>
</font>
</code>

Link to comment
Share on other sites

Manliekas ka to visu var krietni novienkaarshot liidz (shajaa variantaa hailaitojam nevis visu vaardu bet arii fraazi tikai, bet tas taa peec katra oasha veelmeem)

 

$texts = preg_replace('/(?<=>)([^<]+)?('.$search.')/i','$1<span style="background-color: #C0C0C0">$2</span>',$texts);

Link to comment
Share on other sites

×
×
  • Create New...