IT.COM

Help Needed w/ PHP - If/Else

Spaceship Spaceship
Watch

TestCase

Note: Doesn't play well with others.Top Member
Impact
2,381
I'm trying to help out a friend that has been stuck with a site built with a proprietary CMS.

In an effort to help her site's SEO, I've created a few hacks that should improve things, but my overall PHP-fu is weak.

I'm trying to hack the Description meta tag so that it will use a truncated portion of a variable (please excuse me if my nomenclature is incorrect) if that variable exists. If the variable does not exist, an alternate "standard" piece of information should be used.

What I think I'm looking for is an an "if else" sort of thing.

I came up with the following chunks of code that work individually, but I've been unsuccessful in getting them to work together as an "if else" statement.

Display the Variable Description
Code:
<?php
{
echo '<meta property="og:description" content="'.substr(strip_tags($getVARIABLE1['description']),0,110).'" />';
}
?>

Display the "Standard" Description
Code:
<?php
{
echo '<meta property="og:description" content="blah, blah, blah... Boilerplate text..." />';
}
?>

So in a nutshell...

If the variable exists/is not empty then use/display the truncated Variable, otherwise (else) display the Standard description.

Thank you in advance!
 
Last edited:
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
YAY! Got it to work.

There's still a little tweaking that is needed to take out some uneeded spacing and line breaks, but the following code generally works and inserts the appropriate text depending on the situation.

Code:
<?php 
if(isset($getVARIABLE1)){
    echo '<meta property="og:description" content="'.substr(strip_tags($getVARIABLE1['description']),0,110).'" />';
} else{
    echo '<meta property="og:description" content="blah, blah, blah... Boilerplate text..." />';
}
?>
[\code]
 
0
•••
IMPROVED Version of the above code

As mentioned above, I still needed to refine the PHP in order to remove/strip/trim any extra spaces and line breaks so the description tag wouldn't contain anything that was unnecessary.

Here's the latest version that does all that and creates a clean, compact description tag.
Code:
<?php
if(isset($getVARIABLE1))
{
$metadescr = ($getVARIABLE1['description']);
$metadescr = preg_replace("/[\r\n]+/", " ", $metadescr);
$metadescr = preg_replace("/\s+/", ' ', $metadescr);
 echo '<meta property="og:description" content="'.substr(strip_tags($metadescr),0,110).'..." />';
} else {
    echo '<meta property="og:description" content="blah, blah, blah... Boilerplate text..." />';
}
?>
 
Last edited:
0
•••
1
•••
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back