PHP

Convert plain HTML to XHTML

CK1820 
Created at Mar 04, 2013 08:41:00
Updated at Dec 19, 2023 16:17:30 
115   0   0   0  

Below is the example of converting from plain HTML to XHTML.

<?php 
/** 
  * Take a look at the Remark-Lines of the function-header 
  *  
  * function autoAdjustToXHMTL($string) 
  *  
  * Tries to make out of the given string a xhtml-compatible string, 
  * that means that the return-string could be wrapped within a xml. 
  * 
  * Example: $string = "<big><I>something<br></big> blabla" 
  *          return = "<big><i>something><br /></i></big> blabla" 
  * 
  * As you can see, the folowing will be changed: 
  *  - converted "<I>" to "<i>", as all html-nodes will be converted to lowercase 
  *  - converted "<br>" to "<br />" to make br xhtml-conform (same would be done with "img") 
  *  - the closing of "</big>" will have do an implicite closing of "<i>" because <big> surrounds "<i>" 
  * 
  * Beware: the output-string is not the most beautiful code possible but hopefully xhtml-conform 
  * 
  * @param string: input string, html-style (as a browser should like it) 
  * @return: xhtml-compatible string 
  * 
  * Version 
  * V1.0, 20090721, rene@pilz.cc, initial-version 
  * 
  * Released under the terms of BSD (http://en.wikipedia.org/wiki/BSD_licenses) 
*/ 
function autoAdjustToXHTML($string)
{ 
	$returnString=""; 
	$string=trim($string); 
	$elementStack=array(); 
	$stackPoint=0; 
	while ($string!="") { 
		$pos=strpos($string,"<"); 
		if ($pos===false) { 
			// no "<" found, return full string and exit while 
			$returnString.=$string;     
			break; 
		} 

		// copy anythink up to the "<" into return string and reduce string 
		$returnString.=substr($string,0,$pos); 
		$string=trim(substr($string,$pos+1)); 

		$tagName=""; 
		// examine tag-name 
		$i=0; $c=""; 
		for (;$i<strlen($string);$i++) { 
			$c=substr($string,$i,1); 
			if (strpos(";<",$c)!==false) continue;    // some chars we ignore 
			if ($c==" "|| $c==">") break; 
			$tagName.=$c; 
		} 

		$tagName=strtolower($tagName);    // convert uppercase  

		// is there a closing tag? 
		if (substr($tagName,0,1)=="/") { 
			// search for the closing ">" and ignore all before 
			$pos=strpos($string,">"); 
			if ($pos===false) $pos=strlen($string); 
			$string=substr($string,$pos+1); 
			// close as many tags up to the given tag 
			while ($stackPoint>0) { 
				 $stackPoint--; 
				 $stackElement=$elementStack[$stackPoint]; 
				 $returnString.="</".$stackElement.">"; 
				 if ($stackElement==$tagName) break;    // nothing more to do in this while 
			} 
			continue;    // this element-Processing is finished so far, continue at 'while ($string!="") {' 
		} 

		// if we are here, we are within a tag (opening tag) 

		// Push tag on stack 
		$elementStack[$stackPoint]=$tagName; 
		$stackPoint++; 

		// add tag to returnString 
		$returnString.="<$tagName "; 

		// search up to ">" 
		$inApo=false;    // within Apostrophes (" or ') 
		$fakeApo=false; 
		$apoChar=""; 
		for (;$i<strlen($string);$i++) { 
			$c=substr($string,$i,1); 
			if ($fakeApo && strpos(" 	",$c)) { 
				 $fakeApo=false; 
				 $returnString.='"'; 
			} 
			if (!$inApo && !$fakeApo && $c=='=' && !strpos(" 	'"",substr($string,$i+1,1))) { 
				 echo "test2=".substr($string,$i+1,1)."---"; 
				 $fakeApo=true; 
				 $returnString.='="'; 
				 $c=""; 
			} 
			if ($c==$apoChar && $inApo) { $inApo=false; $returnString.=$c." "; $c=""; } 
			if ($inApo && $c=="&" && strpos(substr($string,$i).";",";")>5) $c="&amp;"; 
			else if (($c=="'" || $c=='"') && !$inApo) { $inApo=true; $apoChar=$c; } 
			if ($c==">") break; 
			$returnString.=$c; 
		} 

		// new $string is the rest 
		$string=substr($string,$i+1); 
		$returnString=trim($returnString); 

		// check if this has a "/>" at the end 
		$endSlash=(substr($returnString,strlen($returnString)-1)=="/"); 

		// some elements must have a "/" at the end --> Fake it if needed 
		if (($tagName=="br" || $tagName=="img") && !$endSlash) {  
			$returnString.="/";  
			$endSlash=true; 
		} 

		// check if it is a remark-line (<!-- --> trade this as with-endSlash-Tag) 
		if (substr($tagName,0,3)=="!--") { 
			$returnString=trim($returnString); 
			if (substr($returnString,strlen($returnString)-2)!="--") $returnString.=" --"; // make sure remark-line ends with "-->" 
			$endSlash=true; 
		} 

		// again, do we have a end-slash? (or a faked one?) 
		if ($endSlash) $stackPoint--;    // just remove element from stack 

		// and now add the closing ">" 
		$returnString.=">"; 
	} 

	// ok, we are allmost finish, just clean up the elementStack (from last to first) 
	while ($stackPoint>0) { 
		$stackPoint--; 
		$returnString.="</".$elementStack[$stackPoint].">"; 
	} 
	return $returnString; 
} 
php?>


Tags: PHP URLDecode XHTML autoAdjustToXHTML number_format Share on Facebook Share on X

◀ PREVIOUS
GD library version in PHP
▶ NEXT
Simple PHP code performance measuring example
  Comments 0
Login for comment
SIMILAR POSTS

To block copy & paste on your blog content (created at Mar 03, 2013)

Simple PHP code performance measuring example (updated at Jan 15, 2024)

Simple PHP calendar (created at Jun 02, 2013)

Checking similarity between two strings in PHP (updated at Apr 21, 2024)

GD library version in PHP (updated at Jan 15, 2024)

JSON format control in PHP (updated at Apr 24, 2024)

mysql optimization – if you are using Word Press, do not turn off persistent connection (created at Jun 12, 2012)

File downloading function through PHP code (updated at Dec 17, 2023)

Image file resizing in PHP (updated at Dec 17, 2023)

Apache server unstable detection and the related server(mysql, apache) restart automatically (updated at Dec 17, 2023)

PHP Code can know the current OS is windows or not (updated at Dec 17, 2023)

Function can extract file name in string having full path in PHP (updated at Dec 17, 2023)

How to create private URL on S3/CloudFront with time-limit ? (updated at Dec 17, 2023)

Parsing XML in PHP (created at Mar 06, 2011)

Unzip a ZIP file in PHP (created at Oct 24, 2010)

Uploading files to Amazon S3 with REST API in PHP (updated at Feb 23, 2024)

Human readable random string for captchas in PHP (created at Jul 05, 2010)

Setup Apache+PHP+MySQL on Centos 6.7 (updated at Dec 17, 2023)

How to get browser information in PHP? (created at Jun 22, 2010)

How to get the duration between days in PHP (created at Jun 15, 2010)

Date format validation in PHP (created at Nov 28, 2009)

PHP function converts new line to BR tag (created at Nov 27, 2009)

Email Validation Snipet in PHP (created at Nov 08, 2009)

PHP socket programming to get content with post method (created at Oct 19, 2009)

Get Remote IP Address in PHP (created at Aug 16, 2009)

How to remove HTML tags in text string? (created at Aug 05, 2009)

Directory listing in PHP (created at Jul 06, 2009)

Seconds to String in PHP (created at Jun 06, 2009)

How to load HTML resource on MFC ? (updated at Dec 17, 2023)

How to delete file on certain path ? (created at Oct 01, 2008)

OTHER POSTS IN THE SAME CATEGORY

Mastering Excel Data Importation in PHP (updated at Apr 24, 2024)

Create Blob Image in HTML based on the given Text, Width and Height in the Center of the Image without saving file (updated at Apr 21, 2024)

How do I determine the client IP type (IPv4/IPv6) in PHP (updated at Apr 16, 2024)

What is 302 Found Redirection in HTTP 1.1? (created at Apr 04, 2024)

Implementing a Versatile DNS Server in PHP: Handling A, AAAA, CNAME, and TXT Records (updated at Mar 16, 2024)

Building a Basic DNS Server in PHP/Python: A Beginner's Guide (updated at Mar 15, 2024)

Setup Apache+PHP+MySQL on Centos 6.7 (updated at Dec 17, 2023)

mcrypt missing when using phpMyAdmin on Centos 6.7 (updated at Jan 15, 2024)

PHP Code can know the current OS is windows or not (updated at Dec 17, 2023)

Checking similarity between two strings in PHP (updated at Apr 21, 2024)

Simple PHP calendar (created at Jun 02, 2013)

Simple PHP code performance measuring example (updated at Jan 15, 2024)

GD library version in PHP (updated at Jan 15, 2024)

JSON format control in PHP (updated at Apr 24, 2024)

mysql optimization – if you are using Word Press, do not turn off persistent connection (created at Jun 12, 2012)

File downloading function through PHP code (updated at Dec 17, 2023)

Image file resizing in PHP (updated at Dec 17, 2023)

Apache server unstable detection and the related server(mysql, apache) restart automatically (updated at Dec 17, 2023)

How to set AUTO_INCREMENT ID in mySQL ? (updated at Jan 15, 2024)

Function can extract file name in string having full path in PHP (updated at Dec 17, 2023)

How to create private URL on S3/CloudFront with time-limit ? (updated at Dec 17, 2023)

Parsing XML in PHP (created at Mar 06, 2011)

Unzip a ZIP file in PHP (created at Oct 24, 2010)

Uploading files to Amazon S3 with REST API in PHP (updated at Feb 23, 2024)

Human readable random string for captchas in PHP (created at Jul 05, 2010)

How to get browser information in PHP? (created at Jun 22, 2010)

How to get the duration between days in PHP (created at Jun 15, 2010)

UPDATES

Creating a Pinterest-Style Card Layout with Bootstrap and Masonry (created at Apr 24, 2024)

Mastering Excel Data Importation in PHP (updated at Apr 24, 2024)

JSON format control in PHP (updated at Apr 24, 2024)

Equal Height Blocks in Bootstrap with JavaScript (created at Apr 22, 2024)

How to convert integer to text string ? (updated at Apr 22, 2024)

Checking similarity between two strings in PHP (updated at Apr 21, 2024)

Create Blob Image in HTML based on the given Text, Width and Height in the Center of the Image without saving file (updated at Apr 21, 2024)

How do I determine the client IP type (IPv4/IPv6) in PHP (updated at Apr 16, 2024)

How do I determine the client IP type in Python - IPv4 or IPv6 (updated at Apr 13, 2024)

Getting Started with PyTorch: A Beginner's Guide to Building Your First Neural Network (updated at Apr 09, 2024)

Predicting Buyer Preferences with PyTorch: A Deep Learning Approach (updated at Apr 09, 2024)

Forecasting the Weather with PyTorch: A Beginner's Guide to Temperature Prediction (created at Apr 09, 2024)

PyTorch example to Forcast Stock Price based on 10 days Dataset (created at Apr 09, 2024)

Mastering Model Persistence: Saving and Loading Trained Machine Learning Models in Python (created at Apr 08, 2024)

Harnessing the Power of Random Forest Algorithm in Python (created at Apr 08, 2024)

Understanding and Implementing K-Nearest Neighbors (KNN) Algorithm in Python (created at Apr 08, 2024)

Forecasting with Linear Regression and KNN Regression in Python (updated at Apr 07, 2024)

What is 302 Found Redirection in HTTP 1.1? (created at Apr 04, 2024)

Mastering Random Forest Regression: A Comprehensive Guide with Python Examples (updated at Apr 01, 2024)

Python Implementation of Linear Regression (updated at Apr 01, 2024)

Mastering Supervised Machine Learning with Python: A Comprehensive Guide (created at Apr 01, 2024)

Mastering AI: A Beginner's Guide to Python Programming and Beyond (created at Apr 01, 2024)

How do I create animated background for Google Meet? (updated at Mar 28, 2024)

Building a Simple DNS Server in Delphi with TTL Support (created at Mar 16, 2024)

How to force cookies, disable php sessid in URL ? (updated at Mar 16, 2024)

Implementing a Versatile DNS Server in PHP: Handling A, AAAA, CNAME, and TXT Records (updated at Mar 16, 2024)

Implementing a Versatile DNS Server in Python: Handling A, AAAA, CNAME, and TXT Records (created at Mar 16, 2024)

Building a Basic DNS Server in PHP/Python: A Beginner's Guide (updated at Mar 15, 2024)

Dynamic DNS Made Easy: Building a Python-Based Solution (created at Mar 15, 2024)

Exploring the Depths of Data Transfer: sendfile vs. kTLS (created at Mar 15, 2024)