Re: php string within string compare

2010-06-01 07:38:50

Bill Hernandez

Previous | Next

On Jun 1, 2010, at 9:04 AM, Warren Michelsen wrote:

> <a href="BillOfRights.html"
> <?php
> $requri = getenv ("REQUEST_URI");
> if stristr($requri,"BillOfRights",TRUE)
> echo "class=current";
> ?>
> >The Bill Of Rights</a>

http://php.net/manual/en/function.stristr.php


Your problem appears to be related to the third parameter being set to TRUE

before_needle
If TRUE, stristr() returns the part of the haystack before the first occurrence of theneedle.



I didn't get a chance to look at this on a page, but if you are not using ZendDebugger, I would try this to start :


<a href="BillOfRights.html"
<?php
$requri = getenv ("REQUEST_URI");
if (stristr($requri,"BillOfRights"))
{
echo "class=current_true";
}
else
{
echo "class=current_false";
}
?>
>The Bill Of Rights</a>

> if stristr($requri,"BillOfRights",TRUE)
> echo "class=current";

I don't like the above style, and always use :

if (stristr($requri,"BillOfRights"))
{
echo "class=current_true";
}

or :

if (stristr($requri,"BillOfRights"))
{
echo "class=current_true";
}
else
{
echo "class=current_false";
}

this means that any timeI want to add a line, the block is already setup and there is no confusion.

$result = (stristr($requri,"BillOfRights")) ? "class=\"current_true\"" : "class=\"current_false\"";
echo $result;

which going back to your case :

$result = (stristr($requri,"BillOfRights")) ? "class=\"current\"" : "";
echo $result;
or :

echo (stristr($requri,"BillOfRights")) ? "class=\"current\"" : "";

I didn't get a chance to try this but I think your problem was caused by third parameter being set to TRUE

<a href="BillOfRights.html" <?php echo (stristr($requri,"BillOfRights")) ? "class=\"current\"" : ""; ?> >The Bill Of Rights</a>

Also make sure you have a space after <a href="BillOfRights.html", you could always even add a space before class

" class=\"current\"" (notice the space)

"class=\"current\"" (no space)

These are just some minor things to try...

Good Luck,

Bill Hernandez
Plano, Texas



--
You received this message because you are subscribed to the Google Groups "Web Authoring" group.
To post to this group, send email to web-authoring@[Protected]
To unsubscribe from this group, send email to web-authoring+unsubscribe@[Protected]
For more options, visit this group at http://groups.google.com/group/web-authoring?hl=en.