PHP periyodik unsurları ayrıştırma / html

2 Cevap php

Bu sorun aslında son zamanlarda bana vurdu.

Ben de görüntülenen olacak ne bölümler üzerinde XML tabanlı ve sadece oluşturduğunuz elemanları ile gitti (farklı soru görüşleri sorulmuş) web, insanların bios koyarak ile görevlendirildi.

Bazı insanlar bio formülleri vardı ve ben kopyalama zaman / biçimlendirme üzerinde kopya vermedi yapıştırarak.

My question is that is there an easy way to parse out the formulas and format accordingly?
One idea I had was to just subscript the numbers, but I would have to implement bbcode tags to do this as there are numbers all over the place. Hmm, or I could detect if a number is to the right of a letter and subscript the number.

Forumlas bazıları gibi CoO 3

Ben XML ayrıştırmak için PHP kullanılır.

Sizin görüşleriniz nelerdir?

2 Cevap

Belki böyle bir şey?

<?php
function formatFormulas($html)
{
	$regex  = '/(\\s*(Ac|Ag|Al|Am|Ar|As|At|Au|Ba|Be|Bh|Bi|Bk|Br|B|Ca|Cd|Ce|Cf|Cl|Cm|Co|Cr|Cs|Cu|C|';
	$regex .= 'Db|Ds|Dy|Er|Es|Eu|Fe|Fm|Fr|F|Ga|Gd|Ge|He|Hf|Hg|Ho|Hs|H|In|Ir|I|Kr|K|La|Li|Lr|Lu|Md|';
	$regex .= 'Mg|Mn|Mo|Mt|Na|Nb|Nd|Ne|Ni|No|Np|N|Os|O|Pa|Pb|Pd|Pm|Po|Pr|Pt|Pu|P|Ra|Rb|Re|Rf|Rg|Rh|';
	$regex .= 'Rn|Ru|Sb|Sc|Se|Sg|Si|Sm|Sn|Sr|S|Ta|Tb|Tc|Te|Th|Ti|Tl|Tm|Uub|Uuh|Uuo|Uup|Uuq|Uus|Uut|';
	$regex .= 'U|V|W|Xe|Yb|Y|Zn|Zr)\\s*(<[^>]+>)*\\s*\\d*\\s*(<[^>]+>)*\\s*)+/';
	if ( preg_match_all($regex, $html, $m) ) {

		for ($i = 0; $i < count($m[0]); $i++) {

			$replace = preg_replace('/\\s+/', "", $m[0][$i]);
			$replace = preg_replace('/<[^>]+>/', "", $replace);
			$replace = preg_replace('/\\d+/', '<sub>$0</sub>', $replace);
			$leading = preg_replace('/^(\\s*)[\\S\\s]*/', '$1', $m[0][$i]);
			$trailing = preg_replace('/^[\\S\\s]*?(\\s*)$/', '$1', $m[0][$i]);
			$replace = $leading . $replace . $trailing;
			$html = str_replace($m[0][$i], $replace, $html);

		}

	}

	return $html;
}
?>

Ben kimya gösterimini ayrıştırmak için regex kullanarak doğru yalın olacaktır

Belki bu yardımcı olur? http://www.pmichaud.com/pipermail/pmwiki-users/2008-October/052692.html