RE: [PHP] html parsing question
I've got a little question. I am writing a page where I would like to parse my own invented HTML-looking tags (but want to keep the real HTML tags intact). I use a buffer for the output, and just before the end use ob_get_contents() to get the whole buffer which I want to check for those tags. (Something like: $tag-content = ob_get_contents(); $output = $tag-interpret(); ob_end_clean(); echo $output; ) Now my question is, what is the fastest way to search and replace through this file, whereby the interpretation of the tag can be somewhat complicated? I first just had a loop running character by character through this text, looking for tags, but that is obviously way too slow. Now I have something like: preg_replace_callback (/(.+)/, array($this, 'tag_callback'), $this-content); But then I don't know how to interpret different tags differently. Any suggestions? (A tag looks like: CANTR REPLACE NAME=text where then the whole tag has to be replaced by something called 'text' that has to be looked up in a table. So, CANTR REPLACE NAME=text has to be replaced with something else than CANTR REPLACE NAME=main - well, you get the idea.) Here's an example of how to use preg_replace_callback. Within the callback() function, $matches[1] is going to contain whatever value was in your NAME attribute of your CANTR tag. Act accordingly to it. ?php $text = 'Hello cantr replace text=name. You are on page cantr replace text=main right now.'; function callback($matches) { switch($matches[1]) { case 'name': $retval = 'John'; break; case 'main': $retval = 'Index.php'; break; } return $retval; } $new_text = preg_replace_callback('/cantr replace text=([^]+)/i','callback',$text); echo hr; echo $new_text; ? ---John W. Holmes... PHP Architect - A monthly magazine for PHP Professionals. Get your copy today. http://www.phparch.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] html parsing question
You can output xhtml with your custom tags and use xml parsing functions Jos Elkink wrote: Hello, I've got a little question. I am writing a page where I would like to parse my own invented HTML-looking tags (but want to keep the real HTML tags intact). I use a buffer for the output, and just before the end use ob_get_contents() to get the whole buffer which I want to check for those tags. (Something like: $tag-content = ob_get_contents(); $output = $tag-interpret(); ob_end_clean(); echo $output; ) Now my question is, what is the fastest way to search and replace through this file, whereby the interpretation of the tag can be somewhat complicated? I first just had a loop running character by character through this text, looking for tags, but that is obviously way too slow. Now I have something like: preg_replace_callback (/(.+)/, array($this, 'tag_callback'), $this-content); But then I don't know how to interpret different tags differently. Any suggestions? (A tag looks like: CANTR REPLACE NAME=text where then the whole tag has to be replaced by something called 'text' that has to be looked up in a table. So, CANTR REPLACE NAME=text has to be replaced with something else than CANTR REPLACE NAME=main - well, you get the idea.) Thanks in advance for any help! Jos -- Jos Elkink Game Administration Council Cantr II http://www.cantr.net -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] html parsing question
below is a snippet from a larger code used to capture a daily output graphic file from a site... I put your requirements into it and removed a lot of superfluous stuff, but it is not set to capture text spanning more than one line, but you should be able to modify it accordingly if you can follow the logic. on 8/2/01 5:30 PM, Chuck Barnett at [EMAIL PROTECTED] wrote: Hi I have a question about parsing a page on another server to grab some headlines. I want to search down the page until I find a string -headlines- then I want to grab everything between the next pair of table/table tags. Anyone have a quick solution? i would check out http://php.net/strpos ?PHP /* HTML Content Grabber no copyright */ $file_url = http://www.somewhere.com/;; $grabbed_file = file($file_url); if ($grabbed_file[0]) { for ($i = 0; $i = count($grabbed_file) - 1; $i++) { if($returnstr = strstr ($grabbed_file[$i], '-headlines-')) { $trim = substr ($returnstr, strpos($returnstr, 'table')+7,strpos($returnstr, '/table')-(strpos($returnstr, 'table')+7)); }else{ # not in this line, keep searching } } } else { # Something went wrong } ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] html parsing question
on 8/2/01 5:30 PM, Chuck Barnett at [EMAIL PROTECTED] wrote: Hi I have a question about parsing a page on another server to grab some headlines. I want to search down the page until I find a string -headlines- then I want to grab everything between the next pair of table/table tags. Anyone have a quick solution? i would check out http://php.net/strpos Thanks, Chuck -- mike cullerton -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]