PHP Classes

File: test.php

Recommend this page to a friend!
  Classes of Vagharshak Tozalakyan   Source Parser   test.php   Download  
File: test.php
Role: Example script
Content type: text/plain
Description: Sample
Class: Source Parser
Parsing text with BBCode style tags
Author: By
Last change:
Date: 19 years ago
Size: 2,561 bytes
 

Contents

Class file image Download
<?php

 
require_once('source_parser.class.php');

  function
get_open_tag($attrs)
  {
   
$attr_num = count($attrs);
    for (
$i = 0; $i < $attr_num; $i++)
    {
     
$attrs[$i][0] = strtolower($attrs[$i][0]);
     
$attrs[$i][1] = strtolower($attrs[$i][1]);
    }
   
$replacement = '';
    if (
$attrs[0][0] == 'i')
    {
     
$replacement = '<i>';
    }
    elseif (
$attrs[0][0] == 'style')
    {
     
$bold = false;
     
$font = '';
      for (
$i = 1; $i < $attr_num; $i++)
      {
        if (
$attrs[$i][0] == 'bold' && $attrs[$i][1])
        {
         
$bold = true;
        }
        elseif (
$attrs[$i][0] == 'font')
        {
          if (
$attrs[$i][1] == 'sans-serif')
          {
           
$font = 'Arial, Helvetica, sans-serif';
          }
          else
          {
           
$font = 'Times New Roman, serif';
          }
        }
      }
     
$replacement = '<span style="';
      if (!empty(
$font))
      {
       
$replacement .= 'font-family:' . $font . ';';
      }
      if (
$bold)
      {
       
$replacement .= 'font-weight:bold;';
      }
     
$replacement .= '">';
    }
    elseif (
$attrs[0][0] == 'picture')
    {
      
$replacement = '<img src="' . $attrs[0][1] . '" />';
    }
    return
$replacement;
  }

  function
get_close_tag($tag)
  {
   
$tag = strtolower($tag);
   
$replacement = '';
    if (
$tag == 'i')
    {
     
$replacement = '</i>';
    }
    elseif (
$tag == 'style')
    {
     
$replacement = '</span>';
    }
    return
$replacement;
  }

 
$source = <<<SRC

Here is the text.... plain text, "simple" text... isn't it?
And here is some HTML: <b>Bold</b>

Here is my E-mail: me@myhost.co.uk
And my homepage: http://myhost.co.uk/~me/

Ok :) now let's play with some BBCode style tags...
[STYLE BOLD=1 FONT = 'serif']he, is it cool?[/STYLE]
[i]tags can be case-insensetive[/i]
[ PICTURE = "/images/photo.jpg" ]

SRC;

 
$parser = new source_parser();

// $parser->html_allowed = true;
 
$parser->html_allowed = false;
 
$parser->nl_to_break = true;
// $parser->nl_to_break = flase;
 
$parser->url_parsing_on = true;
// $parser->url_parsing_on = false;
 
$parser->email_parsing_on = true;
// $parser->email_parsing_on = false;
 
$parser->email_encode_on = true;
// $parser->email_encode_on = false;
 
$parser->single_tags = array('picture');
 
$parser->paired_tags = array('style', 'i');
// $parser->paired_tags = array();

 
echo $parser->parse($source, 'get_open_tag', 'get_close_tag');

?>