HELP : this code work in localhost but not in my web page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ahmad
Forum Newbie
Posts: 2
Joined: Mon Aug 25, 2003 6:22 pm

HELP : this code work in localhost but not in my web page

Post by ahmad »

hi all

this code highlight match word in search like this forum search

it is the same when i put it in my program in my localhost it works perfectly but in my site it didnt highlight any search word

the design of code like this

Code: Select all

<?

function phpbb_preg_quote($str, $delimiter)
{
	$text = preg_quote($str);
	$text = str_replace($delimiter, '\'' . $delimiter, $text);
	
	return $text;
}

$highlight_match = $highlight = '';
if (isset($HTTP_GET_VARS['highlight']))
{
	// Split words and phrases
	$words = explode(' ', trim(htmlspecialchars(urldecode($HTTP_GET_VARS['highlight']))));

	for($i = 0; $i < sizeof($words); $i++)
	{
		if (trim($words[$i]) != '')
		{
			$highlight_match .= (($highlight_match != '') ? '|' : '') . str_replace('*', '\w*', phpbb_preg_quote($words[$i], '#'));
		}
	}
	unset($words);

	$highlight = urlencode($HTTP_GET_VARS['highlight']);
}

$message="hi all this is an example of my example ahmad";

	if ($highlight_match)
	{
		// This was shamelessly 'borrowed' from volker at multiartstudio dot de
		// via php.net's annotated manual
		$message = str_replace('"', '"', substr(preg_replace('#(\>(((?>([^><]+|(?R)))*)\<))#se', "preg_replace('#\b(" . $highlight_match . ")\b#i', '<span style="color:#800000"><b>\\\\1</b></span>', '\\0')", '>' . $message . '<'), 1, -1));
	}
echo $message;


?>

u can see
$message="hi all this is an example of my example ahmad";

let we but this code in file with name highlight.php

when i open it , it print this message

but if i write
highlight.php?highlight=example+ahmad

it will print the message but any ahmad and example in the message with

diffrent color

ok this code work in my site and my localhost

but when i put it in my search program

Code: Select all

<?
include "header.php";

$highlight_match = $highlight = '';
if (isset($HTTP_GET_VARS['highlight']))
{
	// Split words and phrases
	$words = explode(' ', trim(htmlspecialchars(urldecode($HTTP_GET_VARS['highlight']))));

	for($i = 0; $i < sizeof($words); $i++)
	{
		if (trim($words[$i]) != '')
		{
			$highlight_match .= (($highlight_match != '') ? '|' : '') . str_replace('*', '\w*', ahmad($words[$i], '#'));
		}
	}
	unset($words);

	$highlight = urlencode($HTTP_GET_VARS['highlight']);
}


if (!$op&&!$sec){
$head="ÎØÃ";
openTable();
echo "æÕæáß ÛíÑ ãÕÑÍ Èå";
closeTable();

}

if ($sec){
$head="ÇáßÊÈ ÇáãæÌæÏÉ Ýí åÐÇ ÇáÞÓã";
openTable();

$a1= mysql_query("select bkid , bkname from ".$prefix."books where secid=$sec");
if (mysql_num_rows($a1)==0){
echo "<br> áÇíæÌÏ ßÊÈ Ýí åÐÇ ÇáÞÓã";
}
 $co=1;
while(list($bkid , $bkname) = mysql_fetch_row($a1)) {

echo "<tr><td >$co )&nbsp;<a style="font-family: tahoma; font-size: 10pt; color: #800000" href="page.php?op=list&bk=$bkid">$bkname</a></td></tr>";
$co++;
}
closeTable();

}

/////////////////////////////////////////////////
if ($op=="list"){
$head="ãÍÊæíÇÊ ÇáßÊÇÈ";
openTable();
$a1= mysql_query("select  fslname , fslid  from ".$prefix."fsl where bkid=$bk order by fslid");
while(list( $fslname , $fslid) = mysql_fetch_row($a1)) {
if ($fslname!="ÝÕá ÊáÞÇÆí")
echo " <tr><td bgcolor="#FDD095" align="center" >$fslname</td></tr>";
$count=1;
$a2= mysql_query("select  bksecid  , bksecname   from ".$prefix."bksections where fslid=$fslid");
while(list( $bksecid  , $bksecname) = mysql_fetch_row($a2)) {
echo "<tr><td > $count ) &nbsp;<a href="page.php?op=show&boksecid=$bksecid">$bksecname</a> </td></tr>";
$count++;
}
}
if (mysql_num_rows($a1)==1 && mysql_num_rows($a2)==0){
echo "ÇáãÍÊæì ÝÇÑÛ";
}
closeTable();
}
///////////////////////////////////////////////////


//////////////////////////////////////////////////
if ($op=="show"){

$a4= mysql_query("select   bksecname ,bksectex   from ".$prefix."bksections where bksecid =$boksecid");
while(list( $bksecname  , $bksectex) = mysql_fetch_row($a4)) {
$head=$bksecname;
openTableno();
	if ($highlight_match)
	{
		$bksectex = str_replace('"', '"', substr(preg_replace('#(\>(((?>([^><]+|(?R)))*)\<))#se', "preg_replace('#\b(" . $highlight_match . ")\b#i', '<span style="color:#800000"><b>\\\\1</b></span>', '\\0')", '>' .  $bksectex . '<'), 1, -1));
	}
echo  $bksectex;

}
closeTable();
}

//////////////////////////////////////////////////


include "footer.php";



?>
it works in my localhost

but nothing happened in my site

could any one help me please

thanx
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

What is $op and $sec ?
Something submitted using $_GET or $_POST ?

If so, see the last link in my signature...
ahmad
Forum Newbie
Posts: 2
Joined: Mon Aug 25, 2003 6:22 pm

Post by ahmad »

every thing is work perfectle

the text is appear but it not highlight the search word

this code not do what should it do it must be highlight

the word in variable $highlight_match (between every word | )

Code: Select all

<?php

 if ($highlight_match) 
   { 
      $bksectex = str_replace('"', '"', substr(preg_replace('#(\>(((?>([^><]+|(?R)))*)\<))#se', "preg_replace('#\b(" . $highlight_match . ")\b#i', '<span style="color:#800000"><b>\\\\1</b></span>', '\\0')", '>' .  $bksectex . '<'), 1, -1)); 
   } 
echo  $bksectex; 


?>
by the way >> this thing make me confuse

cause this code work good and highlight the search word in my local server (phpdev) but it doesnt highlight any word when i uploaded it to my site i am sure it is the same

sooooooo plz help could any body know why
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

I know the idea of doing it all in one line is tempting. But almost always it's not worth the trouble it might cause.
So I began splitting up the m-m-m-mMonster-statement

Code: Select all

<?php
if ($highlight_match)
{
	$bksectex = str_replace('"', '"', substr(preg_replace('#(\>(((?>([^><]+|(?R)))*)\<))#se', "preg_replace('#\b(" . $highlight_match . ")\b#i', '<span style="color:#800000"><b>\\\\1</b></span>', '\\0')", '>' .  $bksectex . '<'), 1, -1));
}
echo  $bksectex;
?>

Code: Select all

<?php
if ($highlight_match)
{
	$subject = substr(preg_replace('#(\>(((?>([^><]+|(?R)))*)\<))#se', "preg_replace('#\b(" . $highlight_match . ")\b#i', '<span style="color:#800000"><b>\\\\1</b></span>', '\\0')", '>' .  $bksectex . '<'), 1, -1);
	$bksectex = str_replace('"', '"', $subject);
}
echo  $bksectex;
?>

Code: Select all

<?php
if ($highlight_match)
{
	$evalReplace = "preg_replace('#\b(" . $highlight_match . ")\b#i', '<span style="color:#800000"><b>\\\\1</b></span>', '\\0')";
	$subject = substr(preg_replace('#(\>(((?>([^><]+|(?R)))*)\<))#se', $evalReplace, '>' .  $bksectex . '<'), 1, -1);
	$bksectex = str_replace('"', '"', $subject);
}
echo  $bksectex;
?>

Code: Select all

<?php
if ($highlight_match)
{
	$evalReplace = "preg_replace('#\b(" . $highlight_match . ")\b#i', '<span style="color:#800000"><b>\\\\1</b></span>', '\\0')";
	$subject = '>' .  $bksectex . '<';
	$subject = preg_replace('#(\>(((?>([^><]+|(?R)))*)\<))#se', $evalReplace, $subject);
	$subject = substr($subject, 1, -1);
	$bksectex = str_replace('"', '"', $subject);
}
echo  $bksectex;
?>
Now you can add debug output (if there's no debugger available)

Code: Select all

<?php
if ($highlight_match)
{
	$evalReplace = "preg_replace('#\b(" . $highlight_match . ")\b#i', '<span style="color:#800000"><b>\\\\1</b></span>', '\\0')";
	echo '<!--', htmlentities($evalReplace), '-->';
	$subject = '>' .  $bksectex . '<';
	echo '<!--', htmlentities($subject), '-->';
	$subject = preg_replace('#(\>(((?>([^><]+|(?R)))*)\<))#se', $evalReplace, $subject);
	echo '<!--', htmlentities($subject), '-->';
	$subject = substr($subject, 1, -1);
	echo '<!--', htmlentities($subject), '-->';
	$bksectex = str_replace('"', '"', $subject);
	echo '<!--', htmlentities($bksectex), '-->';
}
echo  $bksectex;
?>
I haven't tested the changes so you better check them before trying ;)
abeall
Forum Commoner
Posts: 41
Joined: Sun Feb 04, 2007 11:53 pm

Re: HELP : this code work in localhost but not in my web page

Post by abeall »

Sorry to dig up this ancient thread, but I was searching for a way to highlight words in an HTML page and came across this. After some tinkering I came to the conclusion that all you really need is this:

Code: Select all

 
// $terms is an array of words to highlight, so you can highlight more than one word
// $body is the HTML string that you will be highlighting words in and probably outputting later
foreach($terms as $t){
    $offset = 0;
    while(true){
        $pos = stripos($body,$t,$offset);
        if($pos === false) break;
        $replacestr = '<span class="hilite">'.substr($body,$pos,strlen($t).'</span>';
        $body = substr_replace($body,$replacestr,$pos,strlen($t));
        $offset = $pos + strlen($replacestr);
    }
}
 
It just checks for all instances of a word, case insensitive using stripos, and replaces that part of the string using substr_replace with the same substr of the word that was found (so case stays the same) wrapped in some hiliting markup, then continues the search in the next loop starting at the end of the substring that was just replaced, by using an offset for stripos.

Hope someone finds that useful.
Post Reply