You are here: Re: selecting specific tag « PHP Programming Language « IT news, forums, messages
Re: selecting specific tag

Posted by Stephen Kay on 04/04/06 20:37

in article RuSdnfIbrcxVSqzZnZ2dnUVZ_s2dnZ2d@comcast.com, Jerry Stuckle at
jstucklex@attglobal.net wrote on 4/3/06 11:19 PM:

>> OK, I see your point, but did you have a solution to offer? How would you
>> search for this kind of situation, in order to strip out a body tag?
>>
>> Luckily, I don't think any of the pages I need to treat this way have that
>> kind of stuff in them...
>>
>
> First of all, I wouldn't do it like you are. I'd have each page and include
> header and footer files as necessary.
>
> But if I *had* to do this (kicking and screaming) I'd go ahead and delete the
> tags from the files.

I would say that using templates and reading material into content variables
is a pretty standard way of doing things with php. Almost all of the
php-based forums use this method. The advantages are that you can completely
change the layout and appearance of all the pages

If you have a template, you can have a table on the page, with a $navbar
variable, $header, $footer, and $content variables (maybe several of them),
all positioned inside different cells of the table and display all of the
pages of the site in that template. If you suddenly decide you'd like the
navbar on the other side of the page, or the bottom, or to make the page
background a different color or image, you just change to a different master
template.

With the method you are talking about, it seems you would have to change
each page to completely change the layout. Maybe I don't know that much
about it, but I think you are using each page to do the main formatting, and
then just including a header, footer, etc.? The positioning of those
elements is determined by each page, and where the includes are located.
Whereas the positioning of the elements is determined by the template in the
way I'm doing it.

My problem is that I'm trying to adapt an existing straight HTML site to
this other method. Normally, if creating this method from scratch, you
wouldn't put headers, and HTML and BODY tags in the parts that are intended
for content.

Anyway, I now have some code that strips off the header, and removes the
HTML and BODY tags (thanks to all for the suggestions etc.). I realize this
would not be robust for a general solution, but I'm only applying it to one
existing site that has no fancy stuff like nested comments inside the tags
etc.

For anyone who cares (improvements welcome!):

// split the returned source at the end of the /HEAD section
$source_array = preg_split('/(<\/head>)/i', $raw_content);

// if the split fails to find a head tag (like there already
// is no header), the second piece of the array will not be there
if (isset($source_array[1])){ // if content is NOT empty
$headers = "\n<!-- Start Stripped Header\n" . $source_array[0] . "\nEnd
Stripped Header -->\n"; // debug
echo $headers; // debug

$raw_content = $source_array[1];
}else{
echo "<!-- No Header To Strip -->\n"; // debug

$raw_content = $source_array[0];
}
// strip out HTML and BODY tags
$pattern_array = array( '/(<body.*?>)/i',
'/(<\/body>)/i',
'/(<html.*?>)/i',
'/(<\/html>)/i' );

// for debugging; later replace with ""
$replace_array = array( '<!-- body start tag was here -->',
'<!-- body end tag was here -->',
'<!-- html start tag was here -->',
'<!-- html end tag was here -->' );
$raw_content = preg_replace($pattern_array, $replace_array, $raw_content);


--
Stephen Kay
Karma-Lab sk@karma-lab.NOSPAM.com
^^^^^^^

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация