...

View Full Version : Resolved Custom preg_match_all()?


RonnyNishimoto
08-15-2012, 06:36 PM
How would I use preg_match_all in order to have this pattern:


<?php
$file1 = file_get_contents("basketball.txt"); // $file1 = kobe&bryant?dwayne&wade
preg_match_all("&?", $file1, $matches);
$select = array_combine($matches[0], $matches[1]);
?>


In order to output:

$select[0] = "kobe"
$select[1] = "bryant"
$select[2] = "dwayne"
$select[3] = "wade"


Thank you!

Fou-Lu
08-15-2012, 07:28 PM
You don't need to pcre at all, but IMO it does make it faster.
Use a split:

$s = 'kobe&bryant?dwayne&wade';
$split = preg_split('/&|\?/', $s);
print_r($split);

Results in:

Array
(
[0] => kobe
[1] => bryant
[2] => dwayne
[3] => wade
)

RonnyNishimoto
08-15-2012, 07:31 PM
Thanks Fou!

I was getting into weird territory like:


/([a-z])&([a-z])/([a-z])?([a-z])/

safeservicejt
08-16-2012, 04:14 AM
<?php
$text="kobe&bryant?dwayne&wade";

$text=preg_match_all('/(\w+)/i',$text,$matches);

print_r($matches[1]);
?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum