简体   繁体   中英

PHP mysql check inside a function

I have a function (which I did not write) inside an existing php tag in the head of a page that I've been using for several years the parses URL's and email addresses to make them clickable links:

function ParseURLs($str){
  if(isset($str)){
    $Output=strip_tags($str);
    $Output=preg_replace("/(\swww\.)|(^www\.)/i"," http://www.",$Output);
    $Output=preg_replace("/\b(((ftp|http(s?)):\/\/))+([\w.\/&=?\-~%;]+)\b/i"
            ,"<a href='$1$5' target='_blank' rel='nofollow'>$1$5</a>",$Output);
    $Output=preg_replace("/\b([\w.]+)(@)([\w.]+)\b/i"
            , "<a href='mailto:$1@$3'>$1@$3</a>",$Output);
    return nl2br($Output);
  }
}

I wanted to replace the rel='nofollow' with a php check of a MySQL dbase field and have it only put up the rel='nofollow' if the dbase field is empty. I tried to do it by replacing rel='nofollow' in the function with something like this which was my starting point:

<?php if (empty( $row_rswhatever['linkfollow'])) {echo "rel='nofollow'";}?>

or just this:

if (empty( $row_rswhatever['linkfollow'])) {echo "rel='nofollow'";}

I've tried it a hundred different ways (something good usually happens sooner or later) but cannot get it to work. I know from past experience that I am probably missing the boat on more than one issue, and would appreciate any help or guidance. Thanks.

A easy/lazy way to do it would be to continue doing it as you are doing now, however after the last $output=preg_replace add your if test and if you don't want the rel='nofollow' , just use str_replace to remove it.

ie.

function ParseURLs($str)
{
    if(isset($str)){
        $Output=strip_tags($str);
        $Output=preg_replace("/(\swww\.)|(^www\.)/i"," http://www.",$Output);
        $Output=preg_replace("/\b(((ftp|http(s?)):\/\/))+([\w.\/&=?\-~%;]+)\b/i","<a href='$1$5' target='_blank' rel='nofollow'>$1$5</a>",$Output);
        $Output=preg_replace("/\b([\w.]+)(@)([\w.]+)\b/i", "<a href='mailto:$1@$3'>$1@$3</a>",$Output);

        if (empty( $row_rswhatever['linkfollow'])) {
            $Output = str_replace(" rel='nofollow'", "", $Output);
        }

        return nl2br($Output);
    }
}

Without knowing exactly what you'd be checking for in the database:

function ParseUrls($str) {
    $sql = "SELECT ... FROM yourtable WHERE somefield='" . mysql_real_escape_string($str) ."'";
    $result = mysql_query($sql) or die(mysql_error());
    $rel = (mysql_num_rows($result) == 0) ? ' rel="nowfollow"' : '';
    blah blah blah
}

Incidentally, the isset check is useless in your code. The function parameter does not have a default value ( function x($y = default) ), so if no parameter is specified in the calling code, it will cause a fatal error in PHP anyways.

This also assumes that you've already connected to MySQL elsewhere in your code, and are using the mysql library (not mysqli or pdo or db or whatever else).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM