Forum Moderators: coopster

Message Too Old, No Replies

How do you use the IF function to check if a string 'contains'

         

internetheaven

4:29 pm on Feb 13, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have a code and it's not working:

<?php
$pagechecker = $_SERVER['REQUEST_URI'];
if($pagechecker = "pagechange")
{
echo '<META NAME="ROBOTS" CONTENT="INDEX, FOLLOW">';
}
else
{
echo '<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">';
}
?>

any URLs on my site that have "pagechange" as a parameter somewhere is a page adjusted slightly by the user and is therefore a duplicate page essentially. This code in the head tags I thought would be the easiest way to stop these duplicate pages being indexed?

Right now the code always returns the first option even if the URL of the page doesn't contain the parameter "pagechange".

Thanks
Mike

d40sithui

4:45 pm on Feb 13, 2008 (gmt 0)

10+ Year Member



thats cuz you used the assigned operator instead of the comparison operator.
single = instead of == or ===

jatar_k

4:50 pm on Feb 13, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



that's the first issue

this
if($pagechecker = "pagechange")

should be
if($pagechecker == "pagechange")

as mentioned above

you could also look at preg_match [php.net]
try
Example#1 Find the string of text "php"

internetheaven

5:52 pm on Feb 13, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I tried:

if($pagechecker == "pagechange")
and
if($pagechecker === "pagechange")

and all that did was make everything use the 2nd option. Could my problem be with ? The URL that is called is:

/uk/product.php?pagechange=483&ProdID=2

I'm not sure if I've done it right, but:

<?php
$pagechecker = $_SERVER['REQUEST_URI'];
if (preg_match("pagechange", $pagechecker))
{
echo '<META NAME="ROBOTS" CONTENT="INDEX, FOLLOW">';
}
else
{
echo '<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">';
}
?>

Also returns the second result every time.

Thanks
Mike

internetheaven

5:53 pm on Feb 13, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ah! I missed the /'s:

if (preg_match("/pagechange/", $pagechecker))

Thanks all!
MIke

whoisgregg

6:06 pm on Feb 13, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



From the preg_match [php.net] manual page:
Tip
Do not use preg_match() if you only want to check if one string is contained in another string. Use strpos() or strstr() instead as they will be faster.

So switching to something like this would be faster:

if (strpos('pagechange', $pagechecker) !== false)

jatar_k

6:57 pm on Feb 13, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



whoisgregg is right

I admit my first post had strstr and then I changed it to preg_match

I figured pushing you down the regex road early wouldn't hurt you ;)