Find word in string php and create an action within PHP script causes syntax error

–1 vote
asked Aug 2, 2017 by joanne

I found an old PHP whois script which I am going to use on my personal page. The script is working, however I want it to show a link to an external WHOIS (for more details). I did a search on Google and on Stackoverflow and found the following information:

$a = 'How are you?';
if (strpos($a, 'are') !== false) {
   echo 'true';
}

I modified and corrected it (thanks joshua miller) to

if((strpos($url, 'com') !== false) || (strpos($url, 'net') !== false)) {
    echo '<div><a href="https://www.whois.com/whois/'.$url.'">Whois in detail</div>';
}

So if the result of $url containes a .com or .net domain it will show the link, right? Sidenote: $url is already defined elsewhere, so it's an example here to get the ide.

Anyways, now for the issue. I pasted the above text into the current code, but it gives me a syntax error. I am doing something wrong here, but I have no clue what I am doing wrong here.

            if(isset($updation_date) && $updation_date != "" || isset($creation_date) && $creation_date != "" || isset($expiration_date) && $expiration_date != "")
            {               
                $important_dates = '<div class="responsive-table">
                    <table class="table table-hover table-condensed">
                        <thead>
                            <tr>
                                <th colspan="2" class="heading"><h4>'.$_SESSION['IMPORTANT DATES'].'</h4></th>
                            </tr>
                        </thead>
                        <tbody>';
                    if(isset($updation_date) && $updation_date != "")
                    {
                    $important_dates .= '<tr>
                            <th>'.$_SESSION['Updated'].':</th>
                            <td>
                                <span>'.$updation_date.'</span>
                                <span class="c-date">('.domainAge($updation_date).')</span>
                            </td>
                        </tr>';
                    }
                    if(isset($creation_date) && $creation_date != "")
                    {
                    $important_dates .= '<tr>
                            <th>'.$_SESSION['Created'].':</th>
                            <td>
                                <span>'.$creation_date.'</span>
                                <span class="c-date">('.domainAge($creation_date).')</span>
                            </td>
                        </tr>
                        <tr>';
                    }
                    if(isset($expiration_date) && $expiration_date != "")
                    {
                    $important_dates .= '<th>'.$_SESSION['Expires'].':</th>
                            <td>
                                <span>'.$expiration_date.'</span>
                                <span class="c-date">('.domainAge($expiration_date).')</span>
                            </td>
                        </tr>';
                    }
                    $important_dates .= '</tbody>
                        </table>
                    </div>';
            }
            $array[0] = '
                <div class="panel">
                    <div class="panel-heading">
                        <div class="panel-title">
                             <h4><i class="fa fa-globe"></i> '.$url.'</h4>
                        </div>
                    </div>
                </div>

                'if((strpos($url, 'com') !== false) || (strpos($url, 'net') !== false)) {
                     echo '<div><a href="https://www.whois.com/whois/'.$url.'">Whois in detail</div>';
       }'

                '.$list_server.''.$registrar_info.''.$important_dates;
                $array[1] = $raw_registrar_data;
        }

The line I added is located near the end (line #54) as you can see.

Now can someone be so kind to tell me what I am doing wrong here? The error I am getting is a Syntax error at the line with:

'if((strpos($url, 'com') !== false) || (strpos($url, 'net') !== false)) {

...but probably the rest of it is also wrong.

Thank you in advance for your help and advice with this. Probably it's something small, but if you do not know PHP or coding in general, than it can be really hard to figure something out.

Updated with the code joshua miller kindly provided as I used strpos incorrectly in this example. However I am still getting the same syntax error.

Update #2 Apparently the syntax error is because I used an "if"-stament in there, it gives me the following error, when I look it up with a syntax validator: PHP Syntax Check: Parse error: syntax error, unexpected 'if' (T_IF) in your code on line 222 (in the code I paste here it's #54).

5 Answers

+1 vote
answered Aug 2, 2017 by user10089632
if(strpos($url, 'com', 'net')!== false) {

is wrong because it supposes three parameters use instead

if((strpos($url,'com')!== false)||(strpos($url,'com')!== false)) {
+2 votes
answered Aug 2, 2017 by drroach

When using strpos() the syntax is strpos($haystack, $needle) so having two strings like you do: ('com', 'net') is invalid syntax.

You may want to split it into two function calls like so:

if(strpos($url, 'com') !== false || strpos($url, 'net') !== false) {}

+2 votes
answered Aug 2, 2017 by ivo-p

that's not how strpos() works

http://php.net/strpos you can search for 1 needle in your string, not multiple. For that you might want to have a look at preg_match

You can also make it search for "ends in .com" rather than "has com in it somewhere"

if(preg_match('#\.(net|com)$#', $url)) {

}
+3 votes
answered Aug 2, 2017 by joshua-miller

UPDATE: Based on the full code provided, I think the following might help:

    $array[0] = '<div class="panel">
                     <div class="panel-heading">
                         <div class="panel-title">
                             <h4><i class="fa fa-globe"></i> '.$url.'</h4>
                         </div>
                    </div>
                </div>';

    if((strpos($url, 'com') !== false) || (strpos($url, 'net') !== false)) {
        $array[0] .= '<div><a href="https://www.whois.com/whois/'.$url.'">Whois in detail</div>';
    }

    $array[0] .= $list_server.''.$registrar_info.''.$important_dates;

Original Answer

I don't think you can add multiple needles (the term you are searching for) like that in the function. Try this instead:

if((strpos($url, 'com') !== false) || (strpos($url, 'net') !== false)) {
  echo '<div><a href="https://www.whois.com/whois/'.$url.'">Whois in detail</div>';
}
–2 votes
answered Aug 2, 2017 by ksjohn

strpos only takes 3 parameters:

  • whatever you want it to look into,
  • the string it should look for, and
  • the optional offset it should begin at.
Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...