从mysql查询结果的php数组

时间:2014-10-19 10:31:26

标签: php mysql arrays

我尝试做什么? 我试图创建代码,以确定是否有重复数据到数据库,该数据是文本用户写的内容。 所以我做sql查询从数据库中获取行pagetext的结果并从任何符号和坐标(字体颜色,字体大小和字体类型)和图像链接或任何链接清除它,同样我为变量做$post['message']获取文本区域的内容.. 比我检查$post['message']的内容是否与pagetext的内容相同!

完整代码:

// FUNCTION TO CLEAN TEXT
function stripBBCode($text_to_search)
{
 $pattern = '|[[\/\!]*?[^\[\]]*?]|si';
 $replace = '';
 return preg_replace($pattern, $replace, $text_to_search);
}

// MYSQL QUERY
$ckeck = $db->query_read(" SELECT pagetext FROM " . TABLE_PREFIX . " post "); 
$ckeck_num = mysql_num_rows($ckeck); 

            while ($ckeckpagetext = $db->fetch_array($ckeck))
            {

// RESULT FROM QUERY - here i try to make ARRAY BY [square brackets]
$pagetext[] = stripBBCode($ckeckpagetext['pagetext']);
$pagetext[] = preg_replace('/[\s]+/mu','', $pagetext);
$pagetext[] = preg_replace('/\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$]/i', '', $pagetext);

// Variable 
$message = stripBBCode($post['message']);
$message = preg_replace('/[\s]+/mu','',$message );
$message = preg_replace('/\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$]/i', '', $message);

// LOOP
for($x=0; $x<$ckeck_num; $x++)
{
// CHECK IF THERE duplicate TEXT OR NOT
if ($message == $pagetext[$x])
{
$ckeck_duplicate = 1;
}else{
$ckeck_duplicate = 2;
    }
        }
            }

我的问题? 我的代码差不多正确,但我的问题在那些行[方括号]。当我为我的结果尝试很棒的数组时

// RESULT FROM QUERY
$pagetext[] = stripBBCode($ckeckpagetext['pagetext']);
$pagetext[] = preg_replace('/[\s]+/mu','', $pagetext);
$pagetext[] = preg_replace('/\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$]/i', '', $pagetext);

如果我只使用未使用preg_replace的第一行,则代码效果很好。 当我使用[方括号]作为

$pagetext[] = stripBBCode($ckeckpagetext['pagetext']);

2 个答案:

答案 0 :(得分:0)

在变量上尝试您的函数 stripBBCode preg_replace ,例如 $ tmpCheckPageText ,然后将其放入数组 $ pagetext

代码:

$tmpCheckPageText = stripBBCode($ckeckpagetext['pagetext']);
$tmpCheckPageText = preg_replace('/[\s]+/mu','', $tmpCheckPageText);
$tmpCheckPageText = preg_replace('/\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$]/i', '', $tmpCheckPageText); 
$pagetext[] = $tmpCheckPageText;

<强>更新

使用sql语句从db创建pagetext数组,并在while循环中使用清除。例如,你得到了这个:

$pagetext = array( 'Purge Text 1', 'Purge Text 2', 'Purge text 3' );

在此之后,清除您的帖子字段 $ message 。接下来使用 in_array 函数进行检查。

echo in_array( $message, $pagetext ) ? 1 : 2;

答案 1 :(得分:0)

    $pagetext = array();
    while ($ckeckpagetext = $db->fetch_array($ckeck))    
    {
        $tmpCheckPageText = stripBBCode($ckeckpagetext['pagetext']);
        $tmpCheckPageText = preg_replace('/[\s]+/mu','', $tmpCheckPageText);
        $tmpCheckPageText = preg_replace('/\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$]/i', '', $tmpCheckPageText); 
        $pagetext[] = $tmpCheckPageText;
    }

数组是:

$pagetext = array( 'Plain text1', 'Plain text2' );