0

I´ve got a button and a in it variable from my CMS:

<a href="mailto:<?php echo $this->field('contact_person_mail#'.$elementCount)->value(); ?>">

Now I want, to check if the variable is a "http"(a webpage) of an email address and if so, add an "mailto:".

Can you guys help my with that?

4
  • "webpage of an email address"? Commented Feb 7, 2018 at 16:30
  • Yes, a check if the variable is a link to a webpage or an email address Commented Feb 7, 2018 at 16:32
  • There are several ways you can do this, what have you tried? Commented Feb 7, 2018 at 16:33
  • If tried it with "exist" but that was the wrong one. Commented Feb 7, 2018 at 16:34

1 Answer 1

1

You can use:

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(); ?>
">

Since, you haven't mention the name of that variable I am assuming that contact_person_mail could contain both URL or email.

UPDATE:

In case of ternary operator returns FALSE you don't need to echo anything. since your URL or email is already getting echoed after that. Ternary operator is only of printing mailto:.

Sign up to request clarification or add additional context in comments.

3 Comments

So, can I change "$your_variable" to "$this->field('contact_person_mail#'.$elementCount)->value();"? Because that variable can have either a webpage link or email address?
So this would be right? <a href="<?php echo strpos($this->field('contact_person_mail#'.$elementCount)->value(),"http") !== false ? $this->field('contact_person_mail#'.$elementCount)->value(); : "mailto:"; echo $this->field('contact_person_mail#'.$elementCount)->value(); ?> ">
Check the updated code. in case of FALSE you dont need to echo anything. That's what is different in your code and mine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.