如何创建考虑空变量的字符串?

发布于 2024-11-17 15:34:35 字数 746 浏览 0 评论 0原文

我正在尝试为页面构建占位符元描述,以防用户未在 CMS 中包含描述。

我从以下代码开始,但是如果任何其他变量也为空,例如 $phone$location['zip'] 和很快。

<?php   
if (!empty($description)) {
    echo '<meta name="description" content="' .$description . '">';
}
else {
    // Should return: Apple is a business located in Palo Alto, 95014. Call 408.996.1010...
    $description = $name . ' is a ' . strtolower($category) . ' located in ' . $location['city']  . ', ';
    $description .= $location['zip'] . '. Call ' . $phone . ' for more details today.';
    echo '<meta name="description" content="' . $description . '">';        
} ?>

以这种方式构建描述的最有效方法是什么?目前我只能想到嵌套的 if 语句,这听起来很混乱,我确信必须有一个干净的方法来做到这一点。

I'm trying to build a placeholder meta description for a page, in case the user hasn't included a description in the CMS.

I have started with the following code, but of course it fails if any of the other variables are empty too, such as $phone, $location['zip'] and so on.

<?php   
if (!empty($description)) {
    echo '<meta name="description" content="' .$description . '">';
}
else {
    // Should return: Apple is a business located in Palo Alto, 95014. Call 408.996.1010...
    $description = $name . ' is a ' . strtolower($category) . ' located in ' . $location['city']  . ', ';
    $description .= $location['zip'] . '. Call ' . $phone . ' for more details today.';
    echo '<meta name="description" content="' . $description . '">';        
} ?>

What's the most efficient way to build a description in this way? Currently I can only think of nested if statements which sounds messy and I'm sure there must be a clean way to do this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寒江雪… 2024-11-24 15:34:35

添加一个函数来检查值是否已设置?

IE

function checkData($data) {
   if(!empty($data)) {
      return $data;
   } else {
     return '';
   }
}

$description = checkData($name) . ' is a ' . strtolower(checkData($category)) . ' located in ' . checkData($location['city'])  . ', ';

add a function to check if value is set?

i.e.

function checkData($data) {
   if(!empty($data)) {
      return $data;
   } else {
     return '';
   }
}

$description = checkData($name) . ' is a ' . strtolower(checkData($category)) . ' located in ' . checkData($location['city'])  . ', ';
南七夏 2024-11-24 15:34:35

由于描述是根据输入而变化的,因此将其放入自己的函数或类中进行封装:

/**
 * build a description based on various input variables
 * @return string
 */
function build_description($description, $name, $category, array location, $phone) {
   // build the description as you see fit.
}

$description = build_description(compact('description', 'name', 'category'));
$metaDescription = sprintf('<meta name="description" content="%s"', htmlspecialchars($description));

完成后,build_description中的具体实现可以包含很多复杂的语句,而程序的其余部分可以像处理简单的事情一样处理它。

但是,这并没有回答如何在该函数内对其进行编码。但由于该函数的输出数据在很大程度上取决于该函数的输入,因此您只能处理参数确实施加的所有方面。

As the description is something that varies based on the input, put it into a function or class of it's own to encapsulate it:

/**
 * build a description based on various input variables
 * @return string
 */
function build_description($description, $name, $category, array location, $phone) {
   // build the description as you see fit.
}

$description = build_description(compact('description', 'name', 'category'));
$metaDescription = sprintf('<meta name="description" content="%s"', htmlspecialchars($description));

that done, the concrete implementation within build_description can contain a lot of complex statements, while the rest of the program can deal with it as if it is something simple.

However, this does not answer how you could code it inside that function. But as the data of the output of that function heavily depends on the input of that function, you can only deal with all aspects the arguments do impose.

美胚控场 2024-11-24 15:34:35

定义的其他变量不应该是字符串,而应该是对象的一部分,例如...描述。
在这种情况下,调用 Description->isEmpty() 会更容易,如果这些变量之一为空,则返回 true。

如果您坚持这种配置,您仍然可以创建一个数组:
$myArray=数组($name, $category,...);
并检查循环或可能返回 in_array('',$myArray)

The other variables defined shouldn't be string but part of an object such as... Description.
In this case, it would be easier, calling a Description->isEmpty() that returns true if one of those variables are empty.

If you are stuck with this configuration, you still can make an array:
$myArray=array($name, $category,...);
and check in a loop or maybe the return of in_array('',$myArray)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文