Page 1 of 3
regular expressions (STILL NEED HELP (if anyone can))
Posted: Tue Oct 26, 2004 8:46 am
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
Posted: Tue Oct 26, 2004 9:04 am
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
Neither solution works
Posted: Tue Oct 26, 2004 9:17 am
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?
Posted: Tue Oct 26, 2004 10:22 am
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.
Posted: Tue Oct 26, 2004 10:48 am
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.
Posted: Tue Oct 26, 2004 2:11 pm
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++){
$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);
}
Let's change that to:
Code: Select all
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);
}
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.
Posted: Tue Oct 26, 2004 2:44 pm
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?
Posted: Thu Oct 28, 2004 9:42 am
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++ ) {
$find = "/(^| )" . $pattern . "( |$)/";
$output = pregi_replace( $find, "<a href=" ..., $output );
}
Posted: Thu Oct 28, 2004 10:08 am
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?
Posted: Thu Oct 28, 2004 10:18 am
by phpScott
change pregi_replace to eregi_replace.
Posted: Thu Oct 28, 2004 10:25 am
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
Posted: Thu Oct 28, 2004 2:46 pm
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..
Posted: Thu Oct 28, 2004 4:14 pm
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
Posted: Thu Oct 28, 2004 5:41 pm
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?
Posted: Thu Oct 28, 2004 5:46 pm
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