|
I want to parse an html file. For example, say I want to know what is contained inside an <h1> tag. I can do this with strstr and substr but it is ugly.
|
# ¿ Mar 22, 2008 07:16 |
|
|
# ¿ Apr 29, 2024 11:54 |
|
Lumpy posted:If you are using PHP5, and your HTML is valid XML (which it might (probably?) not be) you can do it easily like so: Ah! I tried to use the DOM functions but it was barfing all over the html. This simplexml thing seems a lot more. . . simple. edit: Well simplexml is not liking it either, even after running it through tidy. I think the html is just too big of a mess. I am trying to use preg_match("/<h1>(.*?)<\/h1>/s",$string,$h1); now but I get an error about the regex string: Unknown modifier 'h' edit2: haha, there was a slash in there that you can only see when quoting. It works now! other people fucked around with this message at 21:13 on Mar 22, 2008 |
# ¿ Mar 22, 2008 20:47 |
|
Bonus posted:Don't you mean /<mytag>([^<]*)</mytag>/ Should you not escape the forward slash in the closing tag?
|
# ¿ Mar 28, 2008 18:29 |
|
I am dumb and I am amazed I have never hit this problem before.php:<? if (! $disable_field) { foreach ($form as $field) { if (array_key_exists('disabled', $field)) { unset($field['disabled']); }; # If I print_r $field here 'disabled' is gone! Yay! }: }; # If I print_r here 'disabled' is not gone wtf! ?> edit: ok, I sort of understand what is going on here. $field is a copy of the data in $form. I am just used to reading through arrays or copying relevant stuff into new ones, I guess this is the first time I have actually tried to edit them. So could I not do unset($form[$field]['disabled']) in place of my current unset()? It isn't working either! edit 2: o snap php:<? if (! $disable_field) { foreach ($form as $fieldname => $field) { if (array_key_exists('disabled', $field)) { unset($form[$fieldname]['disabled']); }; }: }; ?> other people fucked around with this message at 18:01 on Apr 19, 2008 |
# ¿ Apr 19, 2008 17:38 |
|
I have a general question about arrays and objects. I have been using code igniter a lot lately and it is (in most cases) happy to accept an object as a function input anywhere that it will take an array. I think my code is a bit easier to read using objects instead of arrays. The syntax of arrays always seemed a bit clunky to me. There are a few places where CI will not take an object and only works with arrays so I have had to break out the odd array here and there throughout my code. It has me thinking now about arrays and objects and if I should be using one over the other. I realize objects can hold more data types but I have never built my own object/class. I don't have any class functions inside my objects. I tried in CI once and failed miserably. I don't know if I was doing it completely wrong or if I wasn't following CI's specifications. So my objects just look like arrays for the most part. In fact, anywhere that I was forced to use an array I always change the variable to an object type as soon as I can to continue working with it. Should I be using objects in this manner? I am just using them to store string variables which I pass between functions for the most part. I just think it looks nicer when you define them, and especially when you have to echo them out into your final html.
|
# ¿ Apr 27, 2008 02:23 |
|
Begby posted:Using objects can help immensely. So you are on the right track, but could get your learn on in the realm of OO. Woah, thank you Begby. That was very helpful! I gave objects another shot and I have my first CI library up and running! I am now on my second library and I have hit a snag. I tried to do this: php:<? class Listing { public $default->display_type = 'all'; public $default->display_order = 'title'; public function read_uri() { $uri_options = $this->ci->uri->uri_to_assoc(3, $default); bla bla bla } } ?> I imagine there is a good reason I cannot set an object like that at the start? How should I do it instead? Makes me wonder if I can set an array. . . edit: I realize now why having an object declared like that is a bad idea. You could declare a variable $what and then also have a function called $what and that would not be good. You can declare arrays, but only if you use the syntax $bla = array ('huh'), doing $bla[] = 'huh' doesn't work. Now I am a bit confused as to what to keep inside the class and what functions should be by themselves! other people fucked around with this message at 05:25 on May 1, 2008 |
# ¿ May 1, 2008 01:59 |
|
noonches posted:So I've got this question, mainly because the people I work with called it sloppy code, and yet my CS teachers from HS and two colleges taught me this. You would have to do break 2; to get out of the if statement and then the foreach. I would use empty() and a do-while loop, but I don't have any formal training at all.
|
# ¿ May 9, 2008 03:24 |
|
I just hit a snag. When you submit a form, if a checkbox is not checked the resulting POST data doesn't have an element name with no value, it doesn't submit anything at all relating to the check box. The script that receives the POST data does not know the name of the checkboxes. This form does a db update, and if the boxes are not checked the resulting fields are never set to zero, they just remain at their old value since they don't appear in the update.
|
# ¿ May 12, 2008 02:39 |
|
Anveo posted:Do you know the names of all the table fields? If so just update them all to zero, and then update them again with the form data. Yes, I can get those. I will have to query for the table field names, and if I don't have a $_POST to match it then it gets set to 0. A bit off topic, but it seems like when submitting forms values all form elements should be sent, even if they are null or blank or disabled etc. There are probably good reasons why they are not, but I have yet to notice them.
|
# ¿ May 12, 2008 03:23 |
|
Why does this only work for the "The " value? If the string starts with "A " or "An " it doesn't catch it.php:<? function format_title($string) { $bit_array = array('The ' => ', The', 'A ' => ', A', 'An ' => ', An'); foreach ($bit_array as $bit => $newbit) { if (strpos($string, $bit) === 0) { $string = ltrim($string, $bit) . $newbit; }; }; } ?>
|
# ¿ May 19, 2008 01:44 |
|
Evil Angry Cat posted:It's the === 0. What is $string = (strpos($string, $prev)==0) ? ltrim($string, $prev) . $new : $string; doing exactly? The if statement is making sure strpos() doesn't return false, and then in that line you are checking to make sure it returns the zero position? I've just never seen syntax like that. And the bit at the end there: ltrim() . $new : $string;? What is the colon doing? Thank you for helping, I am just simple .
|
# ¿ May 19, 2008 03:14 |
|
|
# ¿ Apr 29, 2024 11:54 |
|
fletcher posted:Are you talking about the Ternary Operator? Yes I was. I have never come across it before. That could make for some more concise code. Thank you. minato posted:It works fine for me. Except the function isn't actually returning the modified string. I typed that in the post by hand and forgot that part, the return $string; statement is in the real function and it still doesn't work for me. And I am a complete loving moron! This function was replacing an old function. It is called from two different places and I forgot to change the function name in both places. So in the case I was checking it was calling the old function that just changes the "The " in a string.
|
# ¿ May 19, 2008 04:20 |