Jump to content

Finding Matching Strings within 2 Arrays (having different [keys])


x1705

Recommended Posts

Hi, simply put;
I am having a very difficult time trying to find a way I can Match (partial?) strings, comparing to 2 arrays (the matches have different [Keys].)
for example:

$abs="Bob Rob Sue";
$abt="sometext Bob1:2 moretext Sue3:4 othertext Rob5:6 etc";

how can I put into code, when "Bob1:2" is used, it finds a match of "Bob", and assigns the complete string of "Bob1:2" ?
The course I have been trying I get Bob, Rob, and Sue.. but the #numbers# are in the wrong place. ie; it will result with "Sue5:6"
I understand its a [key] issue, using array_intersect along with some explodes/implodes has helped get me this far.. but i am stumped.

Someway of matching the two arrays coreectly (or keys) would be what i need to figure out.
Heres is what i have now: Thank you for any help, at all. :) cheers.

<?php
$abs="Bob Rob Sue";
$abby="sometext Bob1:2 is Sue3:4 where Rob2:1 etc";


$nums = preg_replace('/[a-zA-Z]/', ' ', $abby);
$lets = preg_replace('/[0-9]:?[^0-9]/', ' ', $abby);

//$lets1=array_intersect($abs, $lets);

$absArr = explode(' ', $abs);
$abbyArr  = explode(' ', $lets);

$letsx=array_intersect($absArr, $abbyArr);


//echo $letsx[0] . "</br>";


//#############################################

$nums1='/(\d+\:\d+)/s';
preg_match_all($nums1, $nums, $nums2);
foreach($nums2 as $numsx){
}

$abi0="$letsx[0]+$numsx[0]";
$abi1="$letsx[1]+$numsx[1]";
$abi2="$letsx[2]+$numsx[2]";

$answer=$abi0."</br>".$abi1."</br>".$abi2."</br>";

echo $answer;



?>

current results:
Bob+1:2
Rob+3:4
Sue+2:1

My desire is that it would Result in:
Bob+1:2
Sue+3:4
Rob+2:1

The #nums are obviously correct.
but the #letters (words/names) obviously are not the same "[key]".

I hope that this is doable..
Thanks in advance. :)

Link to comment
Share on other sites

3 hours ago, x1705 said:

I am having a very difficult time trying to find a way I can Match (partial?) strings, comparing to 2 arrays (the matches have different [Keys].)
for example:

$abs="Bob Rob Sue";
$abt="sometext Bob1:2 moretext Sue3:4 othertext Rob5:6 etc";

how can I put into code, when "Bob1:2" is used, it finds a match of "Bob", and assigns the complete string of "Bob1:2" ?
The course I have been trying I get Bob, Rob, and Sue.. but the #numbers# are in the wrong place. ie; it will result with "Sue5:6"

What?

What output are you trying to get for that, and what process did you (you, personally) follow to get the answer?

Link to comment
Share on other sites

Thanks for the reply. I believe i stated in the original post what 'result' i am after. (towards the bottom).
I am Trying to get the NAME and NUMBERS to be correct.

the last line of above post shows the results i am getting,
since the NAME and number:number are in a different Array,
the keys don't coincide with teh proper element.

 

Quote

current results:
Bob+1:2
Rob+3:4
Sue+2:1

My desire is that it would Result in:
Bob+1:2
Sue+3:4
Rob+2:1


Notice "BOB" is ok (the KEY[0] from Array)
but
ROB and SUE's (numbers) are exchanged.

Hope that helps clarify.  Sorry it wasnt clear enough.
Thanks.

I have personally been trying to resolve this for a few days,
i have as far as i know/to my knowledge tried all the variants (that I can understand)
using array_sort, preg_match_all, array_intersect_key, to just name a few..
I am working on a Regex type solution now.. and splitting fuctions, etc now.

Hope that is clearer.
Thanks for your consideration.
 

Link to comment
Share on other sites

I had to try and explain it to myself also, :) ..here is the simplest way i think i can, if it helps.
--------------------------------------------------------------------------------------------------------------------------------------------

$a1="Bob Sally Joe";

$b2="sometext Bob1:2 here Joe1:1 more text Sally2:3 there."

if ANY "word:number" Combo of $b2 matches a Word in $a1
then get each matching $b2 "word:number combo" into an array OR seperate strings.

ie; would be something like this:

$c1=Bob1:2
$c3=Joe1:1
$c4=Sally2:3

Then secondly, (or perhaps within the same Regex?)

I need to Insert a '+' between each $c strings LAST alpha-character and BEFORE its First Number.

?Do I need to 'split' each $cX at the last alpha character
add a '+'
reassemble $c strings to end result in:

$d1=Bob+1:2
$d3=Joe+1:1
$d4=Sally+2:3

or is there a better, complex 'regex' that can do all of this?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.