Preg_replace_callback with UBB-parser


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. $unparsedText = <<<'EOT'
  4. @cowb: Je moet dit en dit gebruiken....
  5. EOT;
  6.  
  7. function replies( $matches )
  8. {
  9. $username = $matches[1]; // [1] is de eerste match, [2] de tweede, enz.
  10.  
  11. $sQuery = "
  12. SELECT
  13. id
  14. FROM
  15. users
  16. WHERE
  17. username = '".mysql_real_escape_string($username)."'
  18. ";
  19.  
  20. $result = mysql_query($sQuery, $connection);
  21.  
  22. if( !$result && mysql_num_rows($result) > 0 )
  23. {
  24. $row = mysql_fetch_assoc($result) )
  25.  
  26. return '@<a href="'.$result['id'].'">'.$username.'</a>';
  27. }
  28. }
  29.  
  30. $parsedText = preg_replace_callback('/@(.*?):/si', 'replies', $unparsedText);

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.