Jump to content

us2rn4m2

Members
  • Posts

    36
  • Joined

  • Last visited

Everything posted by us2rn4m2

  1. It's a little bit heavy... $xml = <<<XML <?xml version="1.0" encoding="utf-8"?> <Physical> <Catalog> <EquipmentSpec> <Reference>MAT_48985</Reference> <Type>Duplexeur</Type> <Characteristic><CharacteristicName>Tilt</CharacteristicName> <CharacteristicValue>0</CharacteristicValue></Characteristic> </EquipmentSpec> <EquipmentSpec> <Reference>MAT_6890</Reference> <Type>Node</Type> <Characteristic><CharacteristicName>Tilt</CharacteristicName> <CharacteristicValue>2</CharacteristicValue></Characteristic> </EquipmentSpec> <EquipmentSpec> <Reference>MAT_50</Reference> <Type>Single</Type> <Characteristic><CharacteristicName>Tilt</CharacteristicName> <CharacteristicValue>5</CharacteristicValue></Characteristic> </EquipmentSpec> </Catalog> <Installed> <Equipment> <Id>26</Id><Reference>MAT_4</Reference> <Characteristic><CharacteristicName>Height</CharacteristicName><CharacteristicValue>160</CharacteristicValue></Characteristic> </Equipment> <Equipment> <Id>27</Id><Reference>MAT_6890</Reference> <Characteristic><CharacteristicName>Height</CharacteristicName><CharacteristicValue>140</CharacteristicValue></Characteristic> </Equipment> <Equipment> <Id>28</Id><Reference>MAT_50</Reference> <Characteristic><CharacteristicName>Height</CharacteristicName><CharacteristicValue>180</CharacteristicValue></Characteristic> </Equipment> </Installed> </Physical> XML; $e = new SimpleXMLElement($xml); $values = array( 'Type' => array(), 'Id' => array(), 'Height' => array() ); foreach ($e->Catalog->EquipmentSpec as $equipmentSpec) { $values['Type'][] = (string) $equipmentSpec->Type; } foreach ($e->Installed->Equipment as $equipment) { $values['Id'][] = (string) $equipment->Id; foreach ($equipment->Characteristic as $characteristic) { if ($characteristic->CharacteristicName == 'Height') { $values['Height'][] = (string) $characteristic->CharacteristicValue; } } } $sql = array(); foreach($values as $array) { foreach($array as $k => $v) { if(!array_key_exists($k, $sql)) { $sql[$k] = "INSERT INTO machines (`Type`,`Id`,`Height`) VALUES ('" . $values['Type'][$k] . "','" . $values['Id'][$k] . "', '" . $values['Height'][$k] . "')"; // $req = new requete($site->db, $sql[$k]); echo $sql[$k] . '<br>'; /* output: INSERT INTO machines (`Type`,`Id`,`Height`) VALUES ('Duplexeur','26', '160') INSERT INTO machines (`Type`,`Id`,`Height`) VALUES ('Node','27', '140') INSERT INTO machines (`Type`,`Id`,`Height`) VALUES ('Single','28', '180') */ } } } print_r($sql); /* Array ( [0] => INSERT INTO machines (`Type`,`Id`,`Height`) VALUES ('Duplexeur','26', '160') [1] => INSERT INTO machines (`Type`,`Id`,`Height`) VALUES ('Node','27', '140') [2] => INSERT INTO machines (`Type`,`Id`,`Height`) VALUES ('Single','28', '180') ) */
  2. replace foreach ($xml->xpath('Physical') as $spec) by foreach ($xml->xpath('//Catalog') as $spec)
  3. Screenshot: http://img4.hostingpics.net/pics/186791NetbeansThemeWelcome.png Download:https://www.box.com/s/x3fy59klxrbvcv3zwnjz
  4. // Movie foreach($array as $k => $v) { if(is_array($v) && array_key_exists('Movie', $v)) { echo $v['Movie']; } } echo $array['APIParameters']['Movie']; // MovieName foreach($array['MovieReviews'] as $k => $v) { echo $v['MovieName']; } echo $array['MovieReviews'][0]['MovieName'];
  5. echo $feed->sizes->size[5]['id']; // 38
  6. update // check the whole bunch of tags and print "a list" if something is indeed an "infographic" if(in_array('<li><b>infographic</b></li>', $bunch)) { echo implode('', $bunch) . '</ul>'; } $bunch = array(); // add this line // check the whole bunch of tags and print "YES, has an infographic tag" if something is indeed an "infographic" if($tag) { echo 'YES, has an infographic tag'; $tag = null; }
  7. $xml = simplexml_load_file('http://applesold.com/assets/cache/tumblrposts.txt'); $tag = null; $bunch = array(); echo '<ul>'; foreach($xml->posts->post as $post) { if(property_exists($post, 'tag')) { $count = count($post->tag); $i = 0; while($i <= $count) { if(isset($post->tag[$i]) && $post->tag[$i] == 'infographic') { $bunch[$i] = '<li><b>infographic</b></li>'; $tag = true; } else { $bunch[$i] = '<li>non-infographic</li>'; } $i++; } // check the whole bunch of tags and print "a list" if something is indeed an "infographic" if(in_array('<li><b>infographic</b></li>', $bunch)) { echo implode('', $bunch) . '</ul>'; } // check the whole bunch of tags and print "YES, has an infographic tag" if something is indeed an "infographic" if($tag) { echo 'YES, has an infographic tag'; $tag = null; } } } /** Result: non-infographic non-infographic non-infographic non-infographic infographic non-infographic non-infographic non-infographic non-infographic non-infographic non-infographic YES, has an infographic tag */
  8. // ! Different output with PHP 5.3 and 5.4 // PHP 5.4 if(isset($post->tag[$i]) && $post->tag[$i] == 'real estate') { echo '<pre>'; print_r($post->tag); /* SimpleXMLElement Object ( [0] => real estate [1] => vets [2] => Veterans Affairs [3] => VA [4] => home loans [5] => mortgage info [6] => video [7] => Frank Garay [8] => Brian Stevens [9] => buying a house ) SimpleXMLElement Object ( [0] => real estate [1] => dream home [2] => buying a hosue [3] => neighbours [4] => compatibility [5] => lifestyle [6] => Frank Garay [7] => Brian Stevens ) SimpleXMLElement Object ( [0] => real estate [1] => news [2] => hoopla [3] => mortgage rates [4] => video [5] => Frank Garay [6] => Brian Stevens [7] => hype ) SimpleXMLElement Object ( [0] => green [1] => lifestyle [2] => energy conservation [3] => NEST [4] => personal finance [5] => real estate [6] => home systems [7] => Energy Star ) SimpleXMLElement Object ( [0] => real estate [1] => buying a house [2] => internet [3] => www [4] => infographic [5] => home search ) */ // PHP 5.3 echo '<pre>'; print_r($post->tag); /* SimpleXMLElement Object ( [0] => real estate ) SimpleXMLElement Object ( [0] => real estate ) SimpleXMLElement Object ( [0] => real estate ) SimpleXMLElement Object ( [0] => green ) SimpleXMLElement Object ( [0] => real estate ) */
  9. $xml = ' <m:ad xmlns:m="http://www.w3c.org/soap"> <title><![CDATA[TITLE]]></title> <phone>123456789</phone> <attributeGroup> <attribute id="14" name="A1">40</attribute> <attribute id="15" name="A2">50</attribute> </attributeGroup> </m:ad>'; $reader = new XMLReader(); $reader->xml($xml); $id = null; $name = null; while($reader->read() ) { if ($reader->nodeType == XMLReader::ELEMENT && $reader->name == "attribute") { $id = $reader->getAttribute('id'); $name = $reader->getAttribute('name'); printf("id=%s, name=%s\n", $id, $name); } if(isset($id) && isset($name) && $reader->nodeType == XMLReader::TEXT) { printf("value = %s", $reader->value); echo '<br />'; } } /** Result: id=14, name=A1 value = 40 id=15, name=A2 value = 50 */ 2nd method $p = xml_parser_create(); xml_parse_into_struct($p, $xml, $values, $index); xml_parser_free($p); foreach($values as $v) { if(array_key_exists('attributes', $v)) { $attributes = $v['attributes']; if(array_key_exists('ID', $attributes) && array_key_exists('NAME', $attributes)) { echo 'id = ' . $attributes['ID'] . ', '; echo 'name = ' . $attributes['NAME'] . ', '; echo 'value ' . $v['value'] . ' <br />'; } } } /** Result id = 14, name = A1, value 40 id = 15, name = A2, value 50 */
  10. foreach($feed->posts->post as $post) { if(property_exists($post, 'tag')) { $count = count($post->tag); $i = 0; while($i <= $count) { if(isset($post->tag[$i]) && $post->tag[$i] == 'real estate') { // print_r($post->tag); echo '<b>YES</b>'; } else { echo 'no'; } $i++; } } else { // ... } }
  11. Your questions associate to your code are not very clear... So I show you the method that I use to check the presence of "certain tag". // check the presence of 'post' if(property_exists($xml, 'posts')) { echo 'post'; } else { // ... } // check the presence of 'regular-title' if(property_exists($xml->posts->post[0], 'regular-title')) { echo 'regular-title'; } else { // ... } // check the presence of 'tag' foreach($xml->posts->post as $post) { if(property_exists($post, 'tag')) { echo 'tag'; } else { // ... } }
  12. <?php foreach($xml->posts->post as $post) { foreach($post->tag as $tag) { if ($tag == "infographic") { echo "INFOGRAPHIC<br />" ; } else { echo "non-INFOGRAPHIC<br />"; } } }
  13. $xml = ' <ul id="0" class="nestedsortable"> <li id="29"> <div class="itemrow">Home</div> <ul id="29"></ul> </li> <li id="37"> <div class="itemrow">Page 1</div> <ul id="37"> <li id="30"> <div class="itemrow">Page 3</div> <ul id="30"></ul> </li> </ul> </li> <li id="36"> <div class="itemrow">Page 2</div> <ul id="36"></ul> </li> <li id="31"> <div class="itemrow">Page 4</div> <ul id="31"></ul> </li> </ul>'; $array = array(); $id = null; // Iterate the array with some conditions function iterate($a) { global $array; foreach ($a as $k) { if (is_array($k)) { if (array_key_exists('tag', $k) && $k['tag'] == 'UL') { if (array_key_exists('level', $k)) { if (array_key_exists('attributes', $k) && array_key_exists('ID', $k['attributes'])) { if ($k['level'] == 3) { // 3 represent <ul id="29"> || <ul id="37"> || <ul id="36"> || <ul id="31"> $array[0][] = $k['attributes']['ID']; $id = $k['attributes']['ID']; } if ($k['level'] == 5) { // 5 represent <ul id="30"> $array[$id][] = $k['attributes']['ID']; } } } } iterate($k); } } return $array; } // Parse XML data into an array structure - http://php.net/manua...into-struct.php $parser = xml_parser_create(); xml_parse_into_struct($parser, $xml, $values, $index); xml_parser_free($parser); $item = iterate($values); // echo '<pre>'; // print_r($values); // echo '<pre>'; echo '<pre>'; print_r($item); /** Result: Array ( [0] => Array ( [0] => 29 [1] => 37 [2] => 36 [3] => 31 ) [37] => Array ( [0] => 30 ) ) */
  14. I'm not a troller. It was just interresting for me to defends my opinion because I don't forget when I start (alone wihout a teacher) to learn PHP. And before to 'close my mouth', 'No !' I'm not a lier I know Zend: http://forums.zend.c...hp?f=69&t=77938 http://forums.zend.c...p?f=69&t=104538 http://forums.zend.c...hp?f=69&t=72553
  15. Yeah ! That's what I like ! @ignace I totally agree with that ! But, where is the modulo(%) ? It's a school exercice with this 3 following Tip 1: Do not panic - Tip 2: Mostly good solutions also get points - Tip 3: Use modulo (%) @haku Exactly ! Why ? Because I repeat 'Im not a regex expert', so this is the best that I can ! I know PHP, OOP, Design-Patterns and Zend but sure I need some regex lessons ! When you see a little regex like this preg_match('%[A-Z]%', $k) you think it's hard to understand ? @Chritian F I'm open on every critize ! I's good for me ! But you can not to stay behind your screen with a donuts and coffee and just provide a little critic ! Show to us the best that you can do ! If I'm doing something wrong you must to tell me as soon as is possible not when it's too late ! This is the best answer! @Psycho Now, for me, the best solution is not The Best Solution ! If you say "Hey Selena the best solution it's to use the PHP preg_math() function with a complex regex in a single line", you must to tell her "But I know you are a beginner and for the same result I provide you a second way but with some basics PHP functions because it's more easy for you to progress step by step".
  16. I agree @shlumph Thanks @Christian F Post your code and after you can judge me ! @ignace (sorry for my English !) Regex is "magical", but a complex regex it's an advanced concept and it's totally inappropriate for a beginner programmer like Selena to start learning PHP with that sh*t. A beginner need to learn simple and basics concepts, and different kind of way how to manipulate PHP functions !!! function validateLicense($plate) { $p = explode('-', $plate); foreach($p as $k) { if(!preg_match('%^[A-Z0-9]+$%', $k)) { return false; } if(preg_match('%[A-Z]%', $k) && preg_match('%[0-9]%', $k)) { return false; } } return strlen($plate) == 8 ? true : false; }
  17. function validateLicense($plate) { if(substr_count($plate, '-') !== 2) { return false; } if(strrpos($plate, '-') !== 2 && strrpos($plate, '-') !== 5) { return false; } $p = explode('-', $plate); foreach($p as $k) { if(!preg_match('%^[A-Z|0-9]+$%', $k)) { return false; } if(preg_match('%[A-Z]%', $k) && preg_match('%[0-9]%', $k)) { return false; } } return strlen($plate) == 8 ? true : false; } var_dump(validateLicense('AA-12-CD')); if you have something more better than I, please post it !
  18. Selena 1992 say Seriously, you think it's possible for a newbie to write a complex regex ?! :-\ Like I say I'm not a 'regex expert', but sure for my part it's not too late to learn something new. My last code function validateLicense($plate) { $p = explode('-', $plate); foreach($p as $k) { if(preg_match('%[A-Za-z]%', $k) && preg_match('%[a-z0-9]%', $k)) { return false; } if(preg_match('%[A-Z0-9]%', $k)) { continue; } else { return false; } } return strlen($plate) == 8 ? true : false; }
  19. Oops my previous code contains error. I come back !
  20. For a fixed format XX-XX-XX function validateLicense($plate) { $p = explode('-', $plate); foreach($p as $k) { if(preg_match('%[A-Z]%', $k) && preg_match('%[a-z0-9]%', $k)) return false; } return strlen($plate) == 8 ? true : false; } var_dump(validateLicense('AE-12-CD'));
  21. I'm not a 'regex expert', but this code do the job function validateLicense($license) { // A car license plate always consists of 8 characters $strlen = strlen($license); if($strlen !== { return 0; } $i = 0; while($i < $strlen) { // Each character is a letter, a digit or a '-' character if(preg_match('%[A-Z0-9-]%', $license[$i])) { if(isset($license[$i + 1])) { // A letter is always next to another letter or next to a '-' character if(preg_match('%[A-Z]%', $license[$i])) { if(!preg_match('%[A-Z-]%', $license[$i + 1])) { return 0; } } // A digit always stands next to another digit or next to a '-' character if(preg_match('%[0-9]%', $license[$i])) { if(!preg_match('%[0-9-]%', $license[$i + 1])) { return 0; } } } } else { return 0; } $i++; } return 1; }
  22. Hi, Tip 3: Use modulo (%) is a trap ! Ha Ha !! I think you need something like that function validateLicense($license) { // A car license plate always consists of 8 characters if(strlen($license) !== { return false; } $l = str_split($license); // Each character is a letter, a digit or a '-' character foreach($l as $k => $v) { if(!preg_match('%[a-zA-Z0-9_-]%', $v)) { // You see the modulo (%) return false; } } return true; } var_dump(validateLicense('AB-12-CD')); You need to write this condition A digit always stands next to another digit or next to a '-' character. It's your homework ...
  23. // correction $number = "20"; $n = str_split($number); echo $n[0]; echo $n[1];
  24. Hello, $number = "20"; str_split($number); echo $number[0]; echo '<br />'; echo $number[1]; /* Result 2 0 */
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.