Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
other people
Jun 27, 2004
Associate Christ
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.

Adbot
ADBOT LOVES YOU

other people
Jun 27, 2004
Associate Christ

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:

php:
<?
$xml = simplexml_load_file('myHTML.html');
echo $xml->h1;
?>
(Assuming there is only one H1 tag, otherwise $xml->h1 would return an array that you can loop over or just do $xml->h1[0] for the first, etc.)

Otherwise check out the DOM and XML methods in the manual, they are pretty self explanatory.

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

other people
Jun 27, 2004
Associate Christ

Bonus posted:

Don't you mean /<mytag>([^<]*)</mytag>/

Should you not escape the forward slash in the closing tag?

other people
Jun 27, 2004
Associate Christ
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!
?>
Apparently I am working with some copy in my foreach loop? What do I do here?

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']);
        };
    }:
};
?>
in my first try $field is an array, not the key name inside $form, so doing unset($form[$field]['disabled']) isn't going to work or make any sense at all. Thank you irc.

other people fucked around with this message at 18:01 on Apr 19, 2008

other people
Jun 27, 2004
Associate Christ
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.

other people
Jun 27, 2004
Associate Christ

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.

There are lots of things you can do with objects in PHP. The above may not solve your CI solution, it depends on what the functions are doing with the array you pass it, but there are other interfaces such as GetIterator that may solve your problem. You can look them up here http://www.php.net/~helly/php/ext/spl/


Before you do any of that though, brush up on some theory.

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 have a __constructor() function that loads the Code Igniter stuff. That uri_to_assoc() function expects an object as the 2nd param ($default). My problem is that apparently you cannot set objects like that at the start of the class. It throws a fit at the first line inside the class public $default->display_type = 'all';. If I just set $display_type and $display_order it is happy, but then I have to put them into an object inside read_uri() (I guess).

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

other people
Jun 27, 2004
Associate Christ

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.

in this example, it's checking for any blank post values. when one is found, it's supposed to break out of the loop and stop checking values, as it's already found a blank one.
php:
<?
foreach($_POST as $k=>$v)
  {
     if($v=='')
        break;
  }

?>
#1) would the break statement break out of the foreach, or just the IF statement?

#2) is this "sloppy" code? It was taught to me in a basics of comp sci class, and echoed again in a few other classes, so I just assumed it was standard protocol.

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.

other people
Jun 27, 2004
Associate Christ
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.

other people
Jun 27, 2004
Associate Christ

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.

other people
Jun 27, 2004
Associate Christ
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;
          };
     };
}
?>

other people
Jun 27, 2004
Associate Christ

Evil Angry Cat posted:

It's the === 0.

php:
<?
function format_title($string) {
    $bit_array = array('The ' => ', The', 'A ' => ', A', 'An ' => ', An');
    
    foreach ($bit_array as $prev => $new)
    {
        if (strpos($string, $prev)!==FALSE)
        {
            $string = (strpos($string, $prev)==0) ? ltrim($string, $prev) . $new : $string;
        }
    }
    
    return $string;
}
?>
This should work properly

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 :(.

Adbot
ADBOT LOVES YOU

other people
Jun 27, 2004
Associate Christ

fletcher posted:

Are you talking about the Ternary Operator?

http://www.php.net/operators.comparison

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.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply