Convert list of words to array in PHP

This might be just a simple question, but I’m not very familiar in terms of PHP.

I have here a list of words separated by a < br / > tag, example:

Read More
<div id="list">
    gmail.com<br />
    yahoo.com<br />
    yahoo.co.jp
</div>

And, I want to convert it to a list of values in an array looking like this

$acceptedDomains = array('gmail.com', 'yahoo.com', 'yahoo.co.jp');

How can I achieve this output?

I need this actually for domain email checking and here’s how the function works:

            $acceptedDomains = array('gmail.com', 'yahoo.com', 'yahoo.co.jp');
            $email = $current_user->user_email;

            if(in_array(substr($email, strrpos($email, '@') + 1), $acceptedDomains)) {
                //if user is a member, show form.
                echo $email . " is valid" . "<div>" . CFS()->get('mo_email_list') . "</div>";
            }

I want to replace the value of $acceptedDomains to what was listed under the id=”list”

Related posts

6 comments

  1. Try below code:

    <?php
    $input='<div id="list">gmail.com<br />yahoo.com<br />yahoo.co.jp</div>';
    $acceptedDomains = explode("<br />", strip_tags($input, "<br>"));
    print_r($acceptedDomains);
    ?>
    

    Output:

    Array ( [0] => gmail.com [1] => yahoo.com [2] => yahoo.co.jp )

  2. Try this

    <?php
    $input='<div id="list">
        gmail.com<br />
        yahoo.com<br />
        yahoo.co.jp
    </div>';
    $newStr=strip_tags($input, '<br>');
    $acceptedDomains=explode("<br>",$newStr);
    var_dump($acceptedDomains);
    ?>
    

    Above Code will return the output
    as per you want ):

  3. Try this:

    <?php
    $input='<div id="list">
              gmail.com<br />
              yahoo.com<br />
              yahoo.co.jp
            </div>';
    $acceptedDomains=explode("<br />", strip_tags($input, "<br>"));
    print_r($acceptedDomains);
    ?>
    
  4. You can try below code:

    <?php
    $input_string = '<div id="list">gmail.com<br />yahoo.com<br />yahoo.co.jp</div>';
    $strip_tag_str = strip_tags($input_string, "<br>");
    echo "<pre>";
    print_r(explode("<br />", $strip_tag_str));
    echo "</pre>";
    ?>
    

    OUTPUT of Above Code :

    Array
    (
        [0] => gmail.com
        [1] => yahoo.com
        [2] => yahoo.co.jp
    )
    
  5. Simply do:

    <?php $acceptedDomains = explode("<br />", CFS()->get('mo_email_list')); ?>
    

    Or if <div id="list"></div> is also a part of CFS()->get('mo_email_list') output try:

    <?php $acceptedDomains = explode("<br />", strip_tags(CFS()->get('mo_email_list'),"<br>")); ?>
    
  6. Please try below code.

    $data = '<div id="list">
        gmail.com<br />
        yahoo.com<br />
        yahoo.co.jp
    </div>';
    
    $newdatavar=strip_tags($input, '<br />');
    $explodevar=explode("<br />",$newdatavar);
    echo "<pre>";
    print_r($explodevar);
    

Comments are closed.