473,654 Members | 3,107 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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: jt***@jabberafr ica.co.za
Location: South Africa
Time Zone UTC +2
Jul 17 '05 #1
1 1818
"Jens Thiede" <je***********@ webgear.co.za> wrote in message
news:cb******** *@ctb-nnrp2.saix.net. ..
Quick-Fix: Can preg_match_all return the indexes of where it matched the
string?


Yes. Read the manual carefully.
Jul 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4240
by: fartsniff | last post by:
hello all, here is a preg_match routine that i am using. basically, $image is set in some code above, and it can be either st-1.gif or sb-1.gif (actually it randomly picks them from about 100 gifs). then it processes them based off of which image type it selected, either the st- 's or the sb- 's.
8
6975
by: Eric Linders | last post by:
Hi, I'm trying to figure out the most efficient method for taking the first character in a string (which will be a number), and use it as a variable to check to see if the other numbers in the string match that first number. I'm using this code for form validation of a telephone number. Previous records from the past few months show that when someone is just messing around on one of our forms (to waste our time), they type
9
4546
by: Martoni | last post by:
I need to parse a string with an embedded email address. The string always has the format NAME (name@domain) SOMETEXT. What I need to get is the email address as name@domain. I came up with this (I know it's broken, but it's a first start): <?php function ParseTicketEmail($ticket) {
108
6369
by: Bryan Olson | last post by:
The Python slice type has one method 'indices', and reportedly: This method takes a single integer argument /length/ and computes information about the extended slice that the slice object would describe if applied to a sequence of length items. It returns a tuple of three integers; respectively these are the /start/ and /stop/ indices and the /step/ or stride length of the slice. Missing or out-of-bounds indices are handled in a manner...
19
78807
by: Paul | last post by:
hi, there, for example, char *mystr="##this is##a examp#le"; I want to replace all the "##" in mystr with "****". How can I do this? I checked all the string functions in C, but did not find one.
4
14829
by: squash | last post by:
I have a string equal to 'www/' that I want to use in a preg_match. Php keeps giving me the warning: Warning: preg_match(): Unknown modifier '/' How can I escape the string so the / in www/ is not interpreted in the preg_match ?
3
18368
by: deko | last post by:
I'm sure someone has passed this way before... I want to check to see is a domain name is contained in a string, and if one is, I want to extract it. In these strings, domains are always preceded by "http://" or "http : //www" (without the spaces). in pseudo code, I thought it might look like this: if (eregi("http: //", $mystring)) {
4
5249
by: gfrith | last post by:
Hi, A quick regex question which I've worked around for the time being, but would like an answer to if anyone can help. I want to match on all strings which end _id, but not those ending row_id. Some sample strings:
14
2507
by: deko | last post by:
geturl.php Too much code to paste here, but have a look at http://www.liarsscourge.com/ So far, I have not found a string that can break this... Any built-in functions or suggestions for improvement? Thanks in advance.
0
8375
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8290
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8815
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7306
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6161
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5622
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4294
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2714
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1593
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.