PHP 检查变量是否有特定值
PHP check if variable got specific values
我有一个按钮和一个来自我的 CMS 的变量:
<a href="mailto:<?php echo $this->field('contact_person_mail#'.$elementCount)->value(); ?>">
现在我想检查变量是否是电子邮件地址的 "http"(网页),如果是,则添加 "mailto:".
你们能帮我吗?
您可以使用:
strpos — Find the position of the first occurrence of a substring in a string.
<a href="<?php echo strpos($this->field('contact_person_mail#'.$elementCount)->value(),"http") !== false ? "" : "mailto:";
echo $this->field('contact_person_mail#'.$elementCount)->value(); ?>
">
因为您没有提到该变量的名称我假设 contact_person_mail
可以同时包含 URL 或电子邮件。
更新:
如果是三元运算符 returns FALSE,则不需要回显任何内容。因为在那之后你的 URL 或电子邮件已经得到回显。三元运算符只打印mailto:
.
我有一个按钮和一个来自我的 CMS 的变量:
<a href="mailto:<?php echo $this->field('contact_person_mail#'.$elementCount)->value(); ?>">
现在我想检查变量是否是电子邮件地址的 "http"(网页),如果是,则添加 "mailto:".
你们能帮我吗?
您可以使用:
strpos — Find the position of the first occurrence of a substring in a string.
<a href="<?php echo strpos($this->field('contact_person_mail#'.$elementCount)->value(),"http") !== false ? "" : "mailto:";
echo $this->field('contact_person_mail#'.$elementCount)->value(); ?>
">
因为您没有提到该变量的名称我假设 contact_person_mail
可以同时包含 URL 或电子邮件。
更新:
如果是三元运算符 returns FALSE,则不需要回显任何内容。因为在那之后你的 URL 或电子邮件已经得到回显。三元运算符只打印mailto:
.