Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP

Tony Ho
Tony Ho
5,424 Points

explode a string

Is it possible to break up a chemical formula into it's elements with respective subscript? I do not think I can tweak str_split to do the job, I think I would have to search the chemical formula for matches in an array that has the element symbols or something. I just do not know how to connect the array to the non delimited string. For instance how can I split something like NaNO<sub>3</sub>? I am looking for a general way since the formula will change. It seems like I would have to do all types of conditionals. I am focusing my attention by matching to values in an array.

3 Answers

Oliver Gibbs
Oliver Gibbs
9,771 Points

Fun question Tony! That's a tricky one for me and I'm still new to this too but I think it can be done without even needing an array of elements. As long as the chemical formula was formatted correctly using upper and lower case characters could you read through the formula, letter by letter, stopping prior to reaching the next capital letter. For example when I look at NaNO3 I know that Na is one element, N is the next, and O3 is the next as long as upper and lower case is used correctly. Perhaps run it through a loop that stops when it gets to the end of the formula. Hmmm.
The following devolves into pseudocode in the while loop at the end sorry!:

// load chemical formula into a variable:
$chemical_formula = 'NaNO3';

// get length of formula using strlen:
$length_remaining = strlen($chemical_formula);

// create placeholder variable $i and an array to store each individual element;
$i = 0;
$array = array();

while ($length_remaining !=0) {
  //read $chemical_formula letter by letter into $array[$i] (use fgetc??) - Stop prior to reaching a capital letter, so for example $array[0] is assigned Na, $array[1] is N, and $array[2] is O3;
  //increase $i by 1 ($i++);
  //subtract the number of letters processed from the $length_remaining variable;
};

So at the end of this you'd be left with the $array of each of the individual elements. Hope that makes enough sense to use as a starting point!

Tony Ho
Tony Ho
5,424 Points

Thanks! That is a good starting point. I will work on that. I am new to programming and I am seeing that creativity is essential. I will try to work on that today. I will seek to do it as you stated as well as to use matching to values in an array to fetch substring and store into variables. There is so many things I would like to write but I need to learn PHP better. My job gets in the way.

Tony Ho
Tony Ho
5,424 Points

This worked! This also exposed me to something that seems to be very complicated called Perl Compatible Regular Expressions (PCRE). I am not sure if I am allowed to post links to other sites here that goes over this complicated information but it's out there. Anyway, Cheers!

<?php $str= "NaNO3";

$pieces = preg_split('/(?=[A-Z])/',$str);

var_dump($pieces);

?>

output as viewed in page source array(4) { [0]=> string(0) "" [1]=> string(2) "Na" [2]=> string(1) "N" [3]=> string(2) "O3" }

Oliver Gibbs
Oliver Gibbs
9,771 Points

Great, that's way more simple, well done!