简体   繁体   中英

php array from result of mysql query

what i try to do ? i try to make code to found if there duplicate data into database or not ,that data it text Content written by users . so i make sql query to get result of row pagetext from database and clean it from any Signs And coordinate (font color, font size, and font type) and image links or any links , and same i do it for Variable $post['message'] that get Content of text area .. than i check if Content of $post['message'] is same Content of pagetext or not !

Full code :

// 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;
    }
        }
            }

My problem ? my code Almost correct , but my problem in those lines [square brackets] . when i try great array for my result

// 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);

if i used only first line without used preg_replace the code works good . when i use [square brackets] for a

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

Try Your function stripBBCode and preg_replace on variable for example $tmpCheckPageText and after this put into array $pagetext

Code:

$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;

UPDATE:

Create array of pagetext from db using sql statement and purge it in while loop. For example you got this:

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

After this, purge Your post field $message . Next check with in_array function.

echo in_array( $message, $pagetext ) ? 1 : 2;
    $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;
    }

And array is:

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

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