Forum Moderators: coopster

Message Too Old, No Replies

Preg String

no ending clause

         

WhosAWhata

7:18 am on Dec 26, 2004 (gmt 0)

10+ Year Member



i want to search for text that may appear multiple times in a document

$string = "beginning text string";

everytime the $string is found, i want to copy the next 15 characters into an array

ex:
$string = "hi:";
$text = 'Something hi:copy the next 15 characters. some more text hi:the next 15 chars are very important. and more text. hi:yes copy the next 15 characters.';

$array = functionOfMyDreams($string,$text,$someWierdSyntaxStuff);

i want $array to be

$array[0] = "copy the next 1";
$array[1] = "the next 15 cha";
$array[2] = "yes copy the ne";

i hope this makes sense (i know it seems wierd, but i need it)

i'm pretty sure this requires a Preg function, but i don't know the required syntax

Thanks in advance you all

davelms

6:32 pm on Dec 26, 2004 (gmt 0)

10+ Year Member



You're probably right, but I don't know much about preg. I do something very smilar using strpos() to find the text I'm looking for (that's a singular find, but it can go into a while loop to find all occurrences, one at a time) and then extracting the next 'n' chars by using the substr() function, giving it the starting position and the number of chars to copy out. And there you go. Sorry I haven't put example script to explain, hopefully the basics above makes enough sense to give your own code a try. Shout up if you need a sample, or obviously if someone else knows a regex solution that would be better.

coopster

7:31 pm on Dec 26, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I would go with preg_match_all() [php.net]:

$string = "hi:"; 
$text = 'Something hi:copy the next 15 characters. some more text hi:the next 15 chars are very important. and more text. hi:yes copy the next 15 characters.';
$pattern = "/$string(.{15})/Us";
preg_match_all($pattern, $text, $matches);
print_r($matches[1]);

The parentheses capture the string which will be the next 15 of anything after your matched $string.