String indexing and preg_match

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jens Thiede

    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($fi nds, $tmp[1]);
    $remnants = array_merge($re mnants, preg_split($pat tern, $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($needle s as $needle) {
    $pos = strpos($haystac k, $needle, $pos+$l_size);
    array_push($lis t, array(
    "pos" => $pos,
    "raw" => $needle,
    "enc" => $needle));
    $l_size = strlen($needle) ;
    }
    return $list;

    Accurate help much appreciated,

    Jens.

    --
    Jabber ID: jtza7@jabberafr ica.co.za
    Location: South Africa
    Time Zone UTC +2
  • Chung Leong

    #2
    Re: String indexing and preg_match

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

    Yes. Read the manual carefully.


    Comment

    Working...