String indexing and preg_match

This is a discussion on String indexing and preg_match within the PHP Language forums, part of the PHP Programming Forums category; Quick-Fix: Can preg_match_all return the indexes of where it matched the string? More Detail: Read carefully: I'd like ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 06-25-2004
Jens Thiede
 
Posts: n/a
Default String indexing and preg_match

Quick-Fix: Can preg_match_all return the indexes of where it matched the
string?

More Detail: Read carefully: I'd like to seperate a string's parts into two
(2) arrays which can be subdivide themselves. Then modify them and glue
them back together. I will refer to the two arrays as highlight and
remnants. The method I'm using to glue the string together correctly is to
store the string indexes (positions) of both the highlight and remnants
parts of the string before sorting the text into arrays.

ASCII Art Diagram (Mono-space text only):

Original
|
/ \
/ \
/ \
A B
/ \ / \
/ \ / \
AA AB BA BB

HTML entities like example:

Original:

The <html> rain </html> *in* Spain %d %s foo.

Split:

A - Highlight B - Remnants
--------------------------------------
The | <html>
rain | </html>
*in* Spain %d %s foo. |

Glued:

The &lt;html&gt; rain &lt;/html&gt; *in* Spain %d %s foo.

Separation Code:

$finds = array();
$remnants = array();
foreach ($src_array as $src) {
preg_match_all($pattern, $src, $tmp);
$finds = array_merge($finds, $tmp[1]);
$remnants = array_merge($remnants, preg_split($pattern, $src));
}
return array($finds, $remnants);


I'd like to know: what would be the best way to index text directly in the
separation process - to avoid duplicates being miss-indexed?

For example the following function content is bogus because it can't
distinguish which space character (' ') came first:

$pos = 0;
$l_size = 0;
$list = array();
foreach($needles as $needle) {
$pos = strpos($haystack, $needle, $pos+$l_size);
array_push($list, array(
"pos" => $pos,
"raw" => $needle,
"enc" => $needle));
$l_size = strlen($needle);
}
return $list;

Accurate help much appreciated,

Jens.

--
Jabber ID: jtza7@jabberafrica.co.za
Location: South Africa
Time Zone UTC +2
Reply With Quote
  #2 (permalink)  
Old 06-28-2004
Chung Leong
 
Posts: n/a
Default Re: String indexing and preg_match

"Jens Thiede" <jens.spamfree@webgear.co.za> wrote in message
news:cbhdds$m2$1@ctb-nnrp2.saix.net...
> Quick-Fix: Can preg_match_all return the indexes of where it matched the
> string?


Yes. Read the manual carefully.


Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 08:48 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0