php string within string compare

119 views
Skip to first unread message

Warren Michelsen

unread,
Jun 1, 2010, 10:04:58 AM6/1/10
to Web Authoring List

Within an anchor, I want to embed php code to insert the class into it if particular text is within the requested URI.

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

Bill Hernandez

unread,
Jun 1, 2010, 10:38:50 AM6/1/10
to web-au...@googlegroups.com

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>



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

before_needle

If TRUEstristr() 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



Mickey

unread,
Jun 1, 2010, 12:22:08 PM6/1/10
to web-au...@googlegroups.com
At 9:38 AM -0500 6/1/10, Bill Hernandez sent email regarding Re: php string within string compare:
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

Absolutely. A couple more looks at the docs and I realized that.



Through trial and error, I ended up with this:

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




        <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";
        }

Yes, more compact. Thanks.

Bill Hernandez

unread,
Jun 1, 2010, 2:58:51 PM6/1/10
to web-au...@googlegroups.com
I didn't get a chance to run this code, I just wrote it on the fly, but you can test it...

<?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

Reply all
Reply to author
Forward
0 new messages