phppup Posted 20 hours ago Share Posted 20 hours ago Late night and lacking sleep is no time to mingle with stripos. I was playing with some sloppy code and I could use a hand to get it of my mind. Here are the essentials of what I have //a string of HTML to throw into a variable $page ="<h2> item 1 </h2> <p> day </p> <p> time </p> <h2> item 2 </h2> <p> day </p> <p> time </p> <h2> item 3 </h2> <p> day </p> <p> time </p>"; $start = stripos($page, 'h2'); $end = stripos($page, '/h2'); $len = $end - $srart$html_section = $find = substr($page, $start, $len); This worked to get me item 1 However, I'd like to loop through and get the other items too, and eventually the <p> elements also. I am trying to stay within the PHP structure. I was considering a while loop that would increase the pos of the next progressive $start by adding the $len of the previous effort, but I couldn't remember the correct methodology. Guidance and input please. Quote Link to comment https://forums.phpfreaks.com/topic/326704-difficulty-with-stripos/ Share on other sites More sharing options...
requinix Posted 20 hours ago Share Posted 20 hours ago First step to manually parsing HTML is to stop manually parsing HTML. Use DOM instead. 2 Quote Link to comment https://forums.phpfreaks.com/topic/326704-difficulty-with-stripos/#findComment-1649022 Share on other sites More sharing options...
phppup Posted 2 hours ago Author Share Posted 2 hours ago @requinix i stubbornly gave the DOM some research. I got some strange responses so first, but then discovered how (hate to admit it) easy and effective it is. Some insight into is workings might be helpful now. I initially engaged DOM with some "broken" HTML that had already been a little mangled by stripos and offset cuts. When I re-oriented it by <p> it "repaired" my chopped-up <h2> lines. Is this a built-in benefit? How? Eventually I ran the DOM code properly and can see it's effectiveness. I guess at this point it's just a matter of signing the correct loops to gather and distribute the data as I desire. Right? So, are the ole days of stripos and string contents now antiquated? Thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/326704-difficulty-with-stripos/#findComment-1649061 Share on other sites More sharing options...
requinix Posted 1 hour ago Share Posted 1 hour ago 50 minutes ago, phppup said: I initially engaged DOM with some "broken" HTML that had already been a little mangled by stripos and offset cuts. Don't? Just give it the whole document you're working with. 50 minutes ago, phppup said: When I re-oriented it by <p> it "repaired" my chopped-up <h2> lines. Is this a built-in benefit? How? When you did what, it did what with your what? It will make some alterations if necessary to make the document valid, which means if it's doing something then that likely means the source was somewhat incorrect HTML. 50 minutes ago, phppup said: Eventually I ran the DOM code properly and can see it's effectiveness. I guess at this point it's just a matter of signing the correct loops to gather and distribute the data as I desire. Right? Spend a bit of time getting familiar with DOM as a whole (not PHP's implementation, I mean the concept itself) and what it can do. With the sample you posted, I would expect an implementation that (1) finds H2 elements, (2) grabs their nextSiblings for the first paragraph, and (3) grabs their nextSiblings for the second paragraphs. It's also possible to do XPath queries for more advanced searching, but the new DOM API offers querySelector/querySelectorAll which is nearly as powerful. 50 minutes ago, phppup said: So, are the ole days of stripos and string contents now antiquated? 🌎🧑🚀🔫🧑🚀 Always have been. Quote Link to comment https://forums.phpfreaks.com/topic/326704-difficulty-with-stripos/#findComment-1649064 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.