Forum Moderators: coopster

Message Too Old, No Replies

preg_match and intercepting a string before writing to DB

         

Laibcoms

4:07 pm on Mar 14, 2006 (gmt 0)

10+ Year Member



Example - $post_tagging = '<!--tags tag1, tag2, tag3 -->';

then


function addTags( & $content, $Tags, $Before, $After, $Link, $LinkAttributes )
{
$IsFirst = true;
$ContentTags = '';
foreach( $Tags as $Tag )
{
if( strlen( $Tag ) > 1 )
{
if( $IsFirst === true )
$IsFirst = false;
else
$ContentTags .= ' &bull; ';
$TagToDisplay = str_replace( '+', ' ', $Tag );
$ContentTags .= '<a '.$LinkAttributes.' href="'.$Link.$Tag.'">'.$TagToDisplay.'</a>';
}
}
if( strlen( $ContentTags ) > 1 )
{
// Tags added
$content .= $Before.$ContentTags.$After;
return true;
}
else
{
// No tags added
return false;
}
} // addTags

function render( & $content, $format )
{
$AddedAtLeastOneTag = false;

if( preg_match( '/<!--tags?[\s]+([^>]*)[\s]*--\>/si', $content, $Matches ) )
{
if( preg_match_all( '/([^\s]*)[\s]*/si', $Matches[ 1 ], $Matches ) )
{
// Sort using human-like alphabetic order
natcasesort( $Matches[ 1 ] );

// Add miscellaneous tagging systems
$AddedAtLeastOneTag ¦= $this->addTags( $content, $Matches[ 1 ], '<div class="Tags"><strong>Technorati tags:</strong> ', '</div>', 'http://technorati.com/tag/', 'target="_blank" rel="tag"' );
}
}

// No tags found?
global $current_User, $preview;
if( isset( $preview ) && $preview!= false && is_logged_in() &&!$AddedAtLeastOneTag )
{
// Display a warning message
$content .= '<div style="margin:0;padding:16px;color:black;border:solid 4px red;"><p>Tags are missing. To insert a tag, write the following (X)HTML code in your post:</p><blockquote><p><code>&lt;!--tags keyword1 keyword2 keywords3+keywords4 keyword5--&gt;</code></p></blockquote></div>';
}

return true;
}

then data is inserted in DB.

-----------
What we want to happen is to make the $post_tagging be parsed by the two functions addTags and render before it gets written in the DB.

Is it possible?

Thank you very much!

coopster

3:22 pm on Mar 15, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Sure, why not? You could either make the variable global [php.net] in the functions' namespace or you could return the updated variable instead of just TRUE.