Example:
<a href="BillOfRights.html">The Bill Of Rights</a>
would become
<a href="BillOfRights.html" class="current">The Bill Of Rights</a>
if the text "BillOfRights" is within the requested URI.
Is stristr() the right php function to do this?
I tried:
<a href="BillOfRights.html"
<?php
$requri = getenv ("REQUEST_URI");
if stristr($requri,"BillOfRights",TRUE)
echo "class=current";
?>
>The Bill Of Rights</a>
but it doesn't work. Problem is, when I do this, the page stops working right at the point of the php code.
Can stristr() be used to compare a string and a var? Maybe that's the problem.
What's the proper php way to search a string to see if it contains a sub-string? Am I even using the right function?
I'm just getting started with php so be gentle...
<a href="BillOfRights.html"
<?php
$requri = getenv ("REQUEST_URI");
if stristr($requri,"BillOfRights",TRUE)
echo "class=current";
?>
>The Bill Of Rights</a>
If TRUE, stristr() returns the part of the haystack before the first occurrence of theneedle.
if stristr($requri,"BillOfRights",TRUE)
echo "class=current";
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
<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";
}
<?php
$br = "<br>";
$brn = "<br>\n";
function bh_process_url($needle, $haystack, $url_string, $html_page, $true_class, $false_class == "", $return_string = false)
{
$str = "";
if (stristr($haystack,$needle))
{
$str .= "<a href=\"" . $html_page . "\" class=\"" . $true_class . "\">" . $url_string . "</a>"
}
else
{
if($false_class == "")
{
$str .= "<a href=\"" . $html_page . ">" . $url_string . "</a>" // if the $false_class is empty
}
else
{
$str .= "<a href=\"" . $html_page . "\" class=\"" . $false_class . "\">" . $url_string . "</a>"
}
}
if ($return_string) return $str; } else {echo $str; }
}
$needle = "BillOfRights";
$haystack = getenv ("REQUEST_URI");
$url_string = "The Bill Of Rights";
$html_page = $needle . ".html";
$true_class = "current";
$false_class = "not_current";
$return_string = true;
// Call the function this way, where it returns a value
$link = bh_process_url($needle, $haystack, $url_string, $html_page, $true_class, $false_class, $return_string);
echo $link . $brn;
// call the function without its last two parameters (using the default values)
bh_process_url($needle, $haystack, $url_string, $html_page, $true_class);
echo $brn;
?>
Best regards,
Bill Hernandez
Plano, texas