0

I have this bit of PHP code which strips - the dash from the incoming string. The array key may or may not have a - between each word. So if the string has spaces or dashes, the string is still matched to the key.

Now I need to expand this a little to have a partial match locate the key in the array.

Here's what I have so far.

My array

$pages = array(

'Administrator' => array(
    'network-administrator' => array('title' => 'Network '.$li_1, 'description' => 'Network '.$li_1.' '.$temp_content, 'post' => '<p>Network '.$li_1.' '.$temp_content.'.</p>'),
    'database administrator' => array('title' => 'Database '.$li_1, 'description' => 'Database '.$li_1.' '.$temp_content, 'post' => '<p>Database '.$li_1.' '.$temp_content.'.</p>'),
),

'Analyst' => array(
    'business systems analyst' => array('title' => 'Business Systems '.$li_2, 'description' => 'Business Systems '.$li_2.' '.$temp_content, 'post' => '<p>Business Systems '.$li_2.' '.$temp_content.'.</p>'),
    'data-analyst' => array('title' => 'Data '.$li_2, 'description' => 'Data '.$li_2.' '.$temp_content, 'post' => '<p>Data '.$li_2.' '.$temp_content.'.</p>'),
),

);

PHP foreach loop

$t_keys = array();

foreach ($pages as $k => $arr2) {
    foreach (array_keys($arr2) as $a) {
        $new_key = str_replace("-", " ", $a);
        $t_keys[$new_key] = array( $k, $a );
    }
}
$str = str_replace(array('-', ' and '), ' ', strtolower($position));
if (array_key_exists($str, $t_keys)) {
    $target = $pages[ $t_keys[$str][0] ][ $t_keys[$str][1] ];
}

$t_keys[$str][0] accesses the key Administrator and $t_keys[$str][1] accesses sub-key network-administrator.

What I need to do is match incoming strings from the query string in the URL to find matches in the sub-keys of the array.

Example partial match strings that are coming in are oracle database administrator. That key is not in my array, but oracle administrator is, as well as database administrator.

So how could I match strings coming in to the sub-keys in the array if similar words exist?

Here is a small list of incoming partial match strings coming in my error log.

it 
apple 
css 
.net developer
desktop 
.net 
python 
apple 
data center 
phone technician
sql database developer
jquery
delphi
css
python
pc
software
xml
webmaster
research development
python programming
computer technician or pc technician
.net
c  

in some cases, the above partial matches don't exist anywhere in the array. I am just doing a redirect on those. But others such as data center and python programming just as an example are in the array.

Example for the string data center, the nearest match in the array would be data center technician and the nearest match for string python programming would be python developer.

All help appreciated!

EDIT

Just to be clear, here is some more info about partial matching that will be an issue.

yes I know that some matches will be more trickier than others. In my array, there is no key called programming but there is a key called developer so string python programming could be placed to sub key python developer as that is a sub key of the developer key.

Others such as the string data center it will be trickier because there are several sub keys of main keys that have the word data center.

Let's take for example the string computer technician or pc technician. That is no longer a sub-key in my array. but there are sub keys called computer technician as well as pc technician. Which one to partial match if both sub keys exist in the string?

If I can get something basic for now that will atleast take care of some of these partial matches, then I'm on the right track.

EDIT 2

Here's another example of a string business analyst. There is a sub key in my array called business systems analyst. The word business and analyst are both words in the sub key. So I need to create something that I can continue to add onto every time a new string comes in that isn't matching properly in the array.

EDIT 3

After thinking for a few minutes, I think the best way will be to manually add each partial string that comes in to an array and reference those to the sub key of my choosing.

oes that make sense?

So I need to create a php snippet that I keep adding to the array container of partial match strings that point to the key in the array. How can I do that?

9
  • The python programming / python developer "match" is a bit more tricky -- have you thought of any ways to implement that kind of match? Commented Sep 28, 2014 at 18:31
  • yes I know that some matches will be more trickier than others. Posting an edit in my question above to explain this more clearly. Commented Sep 28, 2014 at 18:49
  • I think the best way will be to manually add each partial string that comes in to an array and reference those to the sub key of my choosing. Does that make sense? So a php snippet that I keep adding to the array container of partial match strings that point to the key in the array. How can I do that? Commented Sep 28, 2014 at 19:03
  • 1
    Have you considered using a database instead of PHP arrays? This is going to get unwieldy pretty quickly. Commented Sep 28, 2014 at 19:05
  • My array isn't that big. I choose array because it is easy for me to manage, change, and pull info from. I don't have to worry about the server connection to a database and other issues that come along with having a database. If it gets bigger down the road I may use a database. How do I securely connect to the database? If you have a good reference link for me to check out, I'd appreciate it. Commented Sep 28, 2014 at 19:08

1 Answer 1

1

I wrote a little single word match based on your requirements. So network will match against network whatever stuff goes here because it contains network, etc... I'm not sure if it's what you want, but maybe it'll help you.

class PageWalker{
    public $pages = null;
    public function __construct($pages){
        return $this->pages = $pages;
    }

    public function clean_str($str){
        return strtolower(preg_replace('/[-_\~\!@#\$\%\^\&\*\(\)]/', ' ', $str));
    }

    public function get_results($input=false){
        $results = array();
        foreach($this->pages as $page => $arr):
            foreach($arr as $i => $v):
                if(in_array($this->clean_str($input), explode(' ', $this->clean_str($i)))):
                    $results[$i] = $v;
                endif;
            endforeach;
        endforeach;
        return $results;
    }
}

$walk = new PageWalker($pages);

print_r($walk->get_results('network'));

Here's an eval.in example

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for that. I need something a little different. I may just do simple 301 redirects in htaccess for each partial string that comes in.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.