note 69927 added to function.strpos

From: Date: Wed, 27 Sep 2006 02:33:28 +0000
Subject: note 69927 added to function.strpos
Groups: php.notes 
Request: Send a blank email to php-notes+get-117703@lists.php.net to get a copy of this message
Yay! I came up with a very useful function. This finds a beginning marker and an ending marker (the first after the beginning marker), and returns the contents between them. You specify an initial position in order to tell it where to start looking. You can use a while() or for() loop to get all occurence of a certain string within a string (for example, taking all hyperlinks in a string of HTML code)... function get_middle($source, $beginning, $ending, $init_pos) { $beginning_pos = strpos($source, $beginning, $init_pos); $middle_pos = $beginning_pos + strlen($beginning); $ending_pos = strpos($source, $ending, $beginning_pos + 1); $middle = substr($source, $middle_pos, $ending_pos - $middle_pos); return $middle; } For example, to find the URL of the very first hyperlink in an HTML string $data, use: $first_url = get_middle($data, '<a href="', '"', 0); It's done wonders for scraping HTML pages with certain tools on my website. ---- Server IP: 66.163.161.117 Probable Submitter: 71.88.17.60 ---- Manual Page -- http://www.php.net/manual/en/function.strpos.php Edit -- https://master.php.net/note/edit/69927 Del: integrated -- https://master.php.net/note/delete/69927/integrated Del: useless -- https://master.php.net/note/delete/69927/useless Del: bad code -- https://master.php.net/note/delete/69927/bad+code Del: spam -- https://master.php.net/note/delete/69927/spam Del: non-english -- https://master.php.net/note/delete/69927/non-english Del: in docs -- https://master.php.net/note/delete/69927/in+docs Del: other reasons-- https://master.php.net/note/delete/69927 Reject -- https://master.php.net/note/reject/69927 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#117703) next »