preg_match

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
patty06
Forum Newbie
Posts: 10
Joined: Wed May 03, 2006 1:21 pm

preg_match

Post 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..
someberry
Forum Contributor
Posts: 172
Joined: Mon Apr 11, 2005 5:16 am

Post 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.');
}
patty06
Forum Newbie
Posts: 10
Joined: Wed May 03, 2006 1:21 pm

preg_match

Post 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 ?
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

all patterns must have a slash at the start and end:

Code: Select all

$pattern = '/tomatch/';
with directives after the end slash:

Code: Select all

$pattern = '/tomatch/gi';
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 :

Code: Select all

$pattern = '/<\!\-\-/g';
Be careful if you are using double quoted strings as then you will have to use two escape slashes.

Code: Select all

$pattern = "/<\\!\\-\\-/g";
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.
patty06
Forum Newbie
Posts: 10
Joined: Wed May 03, 2006 1:21 pm

preg_match

Post 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"; 
}
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

that should:

Code: Select all

if(preg_match('/^<\!\-\-.*?'.$recip.'.*?\-\->^/g', $conts))
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

There is no "g" directive in the PCRE functions.
patty06
Forum Newbie
Posts: 10
Joined: Wed May 03, 2006 1:21 pm

preg_match

Post by patty06 »

What do you mean by :

There is no "g" directive in the PCRE functions.[/quote]
patty06
Forum Newbie
Posts: 10
Joined: Wed May 03, 2006 1:21 pm

preg_match

Post 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))
patty06
Forum Newbie
Posts: 10
Joined: Wed May 03, 2006 1:21 pm

preg_match

Post 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"; 
}
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

OK well sorry. I guess i'm thinking of JavaScript here I didn't realise PHP was so different.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Here's a hint: use preg_quote() on something.
patty06
Forum Newbie
Posts: 10
Joined: Wed May 03, 2006 1:21 pm

preg_match

Post by patty06 »

Ahh come on, hint is not going to help me cause this is first time even trying to figure this out.
patty06
Forum Newbie
Posts: 10
Joined: Wed May 03, 2006 1:21 pm

code

Post 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"; 
}
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: preg_match

Post 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.
Post Reply