Page 1 of 1
preg_match
Posted: Wed May 03, 2006 1:32 pm
by patty06
Anyone know how to check for a link if in between comment tags.
This below is not working correctly, so I am assuming preg_match is wrong
$siteurl is site I am checking
and $recip is my url I am looking for.
Example :
Code: Select all
<!--
<a href="http://www.mysite.com">test</a>
-->
$conts = @file_get_contents($siteurl)
if(preg_match("^<!--.*?".$recip.".*?-->^si", $conts))
{
echo "link is in comment tags";
} else {
echo "link is good";
}
Want to make sure my link isnt between comment tags, so if anyone can help me out, would be great..
Posted: Wed May 03, 2006 5:08 pm
by someberry
Wasn't sure whether you wanted to match the *entire* anchor, or just the URL.
Whole anchor:
Code: Select all
preg_match_all('^<!--.*?(<a href="(.*?)">.*?)*?-->^si', $conts, $found);
URL:
Code: Select all
preg_match_all('^<!--.*?<a href="(.*?)">.*?-->^si', $conts, $found);
To do it in an if-statement, something like this (change the regex expression if you want):
Code: Select all
if(preg_match('^<!--.*?<a href="(.*?)">.*?-->^si', $conts))
{
echo('I found a link.');
}
else
{
echo('I didn\'t find a link.');
}
preg_match
Posted: Wed May 03, 2006 6:01 pm
by patty06
But i need to find the url $recip within the comment tags.
I think what you have is just seeing if there is <a href= between the tags.
Or am i missing something ?
Posted: Wed May 03, 2006 6:14 pm
by Ollie Saunders
all patterns must have a slash at the start and end:
with directives after the end slash:
I think you will need the g directive which stands for global.
And you have to escape all regular expression characters so if i want to match <!-- globally :
Be careful if you are using double quoted strings as then you will have to use two escape slashes.
and finally this is the wrong forum, there is one dedicated to regular expressions.
Apologies if I have made any mistakes in this post I don't often use regex so I may have some details wrong.
preg_match
Posted: Wed May 03, 2006 6:29 pm
by patty06
So would this work if I did this ?
Sorry I didnt know this was wrong forum.
Will learn next time..
I added the slashes and the g
Code: Select all
if(preg_match('^<\!\-\-.*?'.$recip.'.*?\-\->^g', $conts))
{
echo "link is in comment tags";
} else {
echo "link is good";
}
Posted: Wed May 03, 2006 6:51 pm
by Ollie Saunders
that
should:
Code: Select all
if(preg_match('/^<\!\-\-.*?'.$recip.'.*?\-\->^/g', $conts))
Posted: Wed May 03, 2006 6:53 pm
by feyd
There is no "g" directive in the PCRE functions.
preg_match
Posted: Wed May 03, 2006 7:23 pm
by patty06
What do you mean by :
There is no "g" directive in the PCRE functions.[/quote]
preg_match
Posted: Wed May 03, 2006 7:34 pm
by patty06
I get this when using the code :
Code: Select all
Warning: preg_match(): Unknown modifier '/'
Code I used was :
Code: Select all
if(preg_match('/^<\!\-\-.*?'.$recip.'.*?\-\->^/g', $conts))
preg_match
Posted: Wed May 03, 2006 7:47 pm
by patty06
None of this has worked for me.
So just in case I missed what I am looking for, here is what I need to do.
I need to find out if
http://www.mysite.com is on a page and within comment tags.
I had this, but it didnt work...
$siteurl is site I am looking for link on
$recip is the url I am looking for on the site to see if in between comment tags
Need the same format as below, but needs to work. This seems really tough.
Can anyone test this problem out to solved it.
Code: Select all
$siteurl="http://www.somesite.com/index.html";
$recip="http://www.mysite.com";
$conts = @file_get_contents($siteurl)
if(preg_match("^<!--.*?".$recip.".*?-->^si", $conts))
{
echo "link is in comment tags";
} else {
echo "link is good";
}
Posted: Thu May 04, 2006 2:22 am
by Ollie Saunders
OK well sorry. I guess i'm thinking of JavaScript here I didn't realise PHP was so different.
Posted: Thu May 04, 2006 9:17 am
by feyd
Here's a hint: use
preg_quote() on something.
preg_match
Posted: Thu May 04, 2006 10:21 am
by patty06
Ahh come on, hint is not going to help me cause this is first time even trying to figure this out.
code
Posted: Mon May 08, 2006 9:47 am
by patty06
Can anyone figure this out at all ?
Code: Select all
$siteurl="http://www.somesite.com/index.html";
$recip="http://www.mysite.com";
$conts = @file_get_contents($siteurl)
// if(preg_match("^<!--.*?".$recip.".*?-->^si", $conts)) //This wont work
if(preg_match("#^<!--.*?".$recip.".*?-->^#si", $conts)) //Neither will this
{
echo "link is in comment tags";
} else {
echo "link is good";
}
Re: preg_match
Posted: Mon May 08, 2006 9:55 am
by Chris Corbyn
patty06 wrote:Ahh come on, hint is not going to help me cause this is first time even trying to figure this out.
Did you actually read that link for preg_quote()

It's more than just a hint - it's your answer.