regular expressions (STILL NEED HELP (if anyone can))

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

mongol
Forum Newbie
Posts: 17
Joined: Tue Oct 26, 2004 8:34 am

regular expressions (STILL NEED HELP (if anyone can))

Post by mongol »

Im a newbie and need some help with eregi_replace. This whole thing doesn't make much sense to me so if you have a solution, can you please explain how you got there? Thank you.

I am trying to create a dynamic glossary function where glossary terms are pulled from a database ($pattern) and matched to the sample text ($sample_text). The sample text is placed where needed with the required link. It all works fine except for the regular expression. I need it to match only when it starts and finishes with the pattern.

Is there anyone who can tell me what I'm doing wrong?

example:
$var=eregi_replace("^$pattern$","<a href=\"http://www.url.com\">\\0</a>",$sample_text);

I've also tried:
$var=eregi_replace("/^$pattern$/","<a href=\"http://www.url.com\">\\0</a>",$sample_text);

PLEASE HELP A NEWBIE BECOME THEIR GOAL!!!

Thank you, thank you, thank you.

Mongol
Last edited by mongol on Tue Oct 26, 2004 12:41 pm, edited 3 times in total.
hokiecsgrad
Forum Newbie
Posts: 17
Joined: Fri Oct 22, 2004 2:55 pm

Post by hokiecsgrad »

I don't completely understand the question, but I'll try to answer what I think you're asking.

You say that you need to match only when the sample text "starts and finishes" with the pattern. If you mean something like this:

case 1:
pattern = "web"
sample text = "web A bunch of computers hooked together web"

on the other hand, you could mean this:
case 2:
pattern = "web"
sample text = "web"

The solution to case 2 is simple:
$find = "^" . $pattern . "$";
$var = eregi_replace( $find, "<a href=\"http://www.url.com\">\\0</a>", $sample_text );

The solution to case 1 should be:
$find = "^" . $pattern . "(.)" . $pattern . "$";
$var = eregi_replace( $find, "<a href=\"http://www.url.com\">\\0</a>", $sample_text );

To help out with Regular Expressions, try the following URL. It comes in really handy when you're trying to debug RegExs.
http://www.quanetic.com/regex.php
mongol
Forum Newbie
Posts: 17
Joined: Tue Oct 26, 2004 8:34 am

Neither solution works

Post by mongol »

Thank you for trying but it didn't work. For some reason ^ and $ are not working. Let me go into a little more detail.

Say the glossary phrase is 'agonist' and the phrase 'antagonist' appears in the text, I do not want it matched.

I the glossary phrase is 'repetition' and the phrase 'repetition count' is in the text, I do not want it matched.

I only want to match the phrase that is in the glossary exactly.
Is there a way to do this?
hokiecsgrad
Forum Newbie
Posts: 17
Joined: Fri Oct 22, 2004 2:55 pm

Post by hokiecsgrad »

What exactly isn't working? Is it not matching? Is it not providing the expected output? Can you give me a few more examples of things that should match versus things that shouldn't? I need to see the full $sample_text variable as well as the full $pattern variable. And maybe posting some of your code would help. The error might not be with the regular expression itself.
mongol
Forum Newbie
Posts: 17
Joined: Tue Oct 26, 2004 8:34 am

Post by mongol »

Here's the complete function. Don't be too hard on me, I'm a newbie remember.

It needs to work for any word or phrase you can imagine (but no special character (I'll work that one out later)):

function text($page_name,$page_vars,$sample_text){
global $database,$link,$baseref;
$page_vars_format="";
if(count($page_vars)){$run_through=1;
while(list($key,$value)=each($page_vars)){
if($run_through==1){$page_vars_format.="?";$run_through=2;}else{$page_vars_format.="&";}
$page_vars_format.=$key;
$page_vars_format.="=";
$page_vars_format.=$value;
}
}
//gather glossary
$gather_glossary_query="select glossary_id,
glossary_term
from glossary
order by glossary_term desc";
$gather_glossary_result=mysql_db_query($database,$gather_glossary_query,$link);
$gather_glossary_number=mysql_num_rows($gather_glossary_result);
$output = stripslashes($sample_text);
for($i=0;$gather_glossary_number>$i;$i++){
$gather_glossary_data=mysql_fetch_object($gather_glossary_result);
$glossary_id=$gather_glossary_data->glossary_id;
$glossary_term=$gather_glossary_data->glossary_term;
$arr_glossary_id[]=$glossary_id;
$arr_glossary_term[]=$glossary_term;
}
for($j=0;count($arr_glossary_id)>$j;$j++){
$find = "^".$arr_glossary_term[$j]."$";
$output=eregi_replace($find,"<a href=\"".$baseref.$page_name."\" onClick=\"MM_openBrWindow('".$baseref."software/glossary.php?action=start&f_glossary_id=".$arr_glossary_id[$j]."','glossary','scrollbars=yes,width=450,height=600')\" class=\"std_glossary_link\">\\0</a>",$output);

}
printf ("%s\n",$output);
}

example of use:
text('index.php',$HTTP_GET_VARS,'This is a test - antagonist Antagonist agonist Agonist repetition count whatever you can think of.');

Thanks for your continued help.
hokiecsgrad
Forum Newbie
Posts: 17
Joined: Fri Oct 22, 2004 2:55 pm

Post by hokiecsgrad »

Ah ha!! =) I finally understand what it is you're trying to do. Sorry it took me so long, I should have picked up on this earlier. Okay, you are definitely on the right track. Let's just adjust that final loop and regex a bit.

Right now you've got:

Code: Select all

for($j=0;count($arr_glossary_id)>$j;$j++)&#123;
  $find = "^".$arr_glossary_term&#1111;$j]."$";
  $output=eregi_replace($find,"<a href="".$baseref.$page_name."" onClick="MM_openBrWindow('".$baseref."software/glossary.php?action=start&f_glossary_id=".$arr_glossary_id&#1111;$j]."','glossary','scrollbars=yes,width=450,height=600')" class="std_glossary_link">\\0</a>",$output);
&#125;
Let's change that to:

Code: Select all

for($j=0;count($arr_glossary_id)>$j;$j++)&#123;
  $find = " ".$arr_glossary_term&#1111;$j]." ";
  $output=eregi_replace($find,"<a href="".$baseref.$page_name."" onClick="MM_openBrWindow('".$baseref."software/glossary.php?action=start&f_glossary_id=".$arr_glossary_id&#1111;$j]."','glossary','scrollbars=yes,width=450,height=600')" class="std_glossary_link">\\0</a>",$output);
&#125;
All I did was take out the carat (^) and dollar sign ($) from the regular expression and replace each with a space. What you want to do is find the whole word or phrase inside of a string.

For instance, if your glossary term was "web" you would want to match "web" but not "spiderweb". If you change your find string to " web " then your are sure to only find the whole word "web" inside of the string and not partial words. I hope that makes sense.
mongol
Forum Newbie
Posts: 17
Joined: Tue Oct 26, 2004 8:34 am

Post by mongol »

I thought of that but it doesn't work. If you have a word or phrase at the start or end of a sentence there are no spaces!!

Any other ideas?
hokiecsgrad
Forum Newbie
Posts: 17
Joined: Fri Oct 22, 2004 2:55 pm

Post by hokiecsgrad »

Doh! You got me. Let's change the regular expression replacement line to this:

Code: Select all

for( $j = 0; count( $arr_glossary_id ) > $j; $j++ ) &#123;
   $find = "/(^| )" . $pattern . "( |$)/";
   $output = pregi_replace( $find, "<a href=" ..., $output );
&#125;
mongol
Forum Newbie
Posts: 17
Joined: Tue Oct 26, 2004 8:34 am

Post by mongol »

Thanks for not giving up on me!

I'm getting this error message:

Fatal error: Call to undefined function: pregi_replace()

Any more ideas?
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

change pregi_replace to eregi_replace.
mongol
Forum Newbie
Posts: 17
Joined: Tue Oct 26, 2004 8:34 am

Post by mongol »

for( $j = 0; count( $arr_glossary_id ) > $j; $j++ ) {
$find = "/(^| )" . $arr_glossary_term[$j] . "( |$)/";
$output = eregi_replace( $find, "<a href...", $output );
}

Doesn't work. It doesn't link any of the glossary terms. My "guess" is that it is not matching any of the results.

I'm really grateful for everyone's help.
I know that when I sort this out, it will be a very useful function. So don't give up on me yet!!

Mongol
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

PLEASE explain exactly what you want. I'm getting tid bits here and there from reading the topic but it's not making sense.

Are you trying to search a list of words for something? I'm confused..
mongol
Forum Newbie
Posts: 17
Joined: Tue Oct 26, 2004 8:34 am

Post by mongol »

I will try to explain it as clearly as I can.

On the website I've made I want to turn any glossary words in the text of my site into a hyperlink. That hyperlink will open a page that will contain the word as a heading and the description of that word. The glossary terms are held on a MySql database that is updated from a user interface located in an administration area of the website.

This sounds fairly simple, and when I started writing the function I was confident that it would not be a problem. BUT, the further you get into all the ins and outs, the more complicated it becomes.

My code serves the function the glossary terms in reverse order so the longer words get tested first and I wanted the function to match the term completely and accurately, but so far, I not having much luck.

I am now starting to feel that there is no way to produce the result I'm looking for.

If anyone can help, I will be most grateful.

Mongol
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

Why not explode the page into an array (by spaces), then get all the glossary terms from the db into another array, then loop through the first, using in_array to check whether its a glossary word or not, if so, printing out the link?
mongol
Forum Newbie
Posts: 17
Joined: Tue Oct 26, 2004 8:34 am

Post by mongol »

Because not all the words are single words. I should have used the word glossary term. For example:

Breathing Technique
Cardio Equipment
Cardiovascular Exercise
Exercise Equipment
Post Reply