0

I'm trying to extract and assign values of CEO, Chairman and any other key positions to distinct variables. I was wondering how this can be done and what the best method to use is. Please see my code below which has a variable $keypeople but I'd like this to be further broken down into variables $CEO; $Chairman etc. depending on the roles contained in $keypeople. In the example below the $keypeople variable returns the following string:

key_people = {{unbulleted list|[[Larry Page]] ([[CEO]])|[[Eric Schmidt]] ([[Chairman]])|[[Sergey Brin]] (Director of [[Google X]] and Special Projects){{cite web|url=https://www.google.co.uk/intl/en/about/company/facts/management/ |title=Management Team - Company - Google}}}} 

Any assistance in much appreciated.

<html>
<body>
<h2>Search</h2>
<form method="post">
Search: <input type="text" name="q" value="Google"/>
<input type="submit" value="Submit">
</form>

<?php
if (isset($_POST['q'])) {
$search = $_POST['q'];
$search = ucwords($search);
$search = str_replace(' ', '_', $search);
$url_2 = "http://en.wikipedia.org/w/api.php?
action=query&prop=revisions&rvprop=content&format=
json&titles=$search&rvsection=0&continue=";
$res_2 = file_get_contents($url_2);
$data_2 = json_decode($res_2);

?>

<h2>Search results for '<?php echo $search; ?>'</h2>

<?php foreach ($data_2->query->pages as $r): 

?>


<?php foreach($r->revisions[0] as $a); 

if (preg_match_all('/key_people += (.*)/', $a, $result)) {

$keypeople = trim($result[0][0]);

echo $keypeople;
} else {
echo 'Not found';
}

?>

<?php endforeach;

?>

<?php 
}
?>

</body>
</html>
9
  • If you got a json why are you using preg_match? Oo Commented Dec 23, 2014 at 8:28
  • What does $keypeople look like? Can you trim your question down to just the code that has to do with this? Commented Dec 23, 2014 at 8:31
  • Instead of separate variables, why don't you extract the data into an associative array? Commented Dec 23, 2014 at 8:32
  • @Barmar what is the best way to get the associative array? Also $keypeople is a string in this case. Sorry I'm unclear as to how you would like the question to be trimmed? Commented Dec 23, 2014 at 8:41
  • I know it's a string, what's in the string? Please show examples. If the string is something like role=name, role=name, you can use explode(',') to split it up, then explode('=') to break these apart into the role and name, then use those to assign to the associative array. Commented Dec 23, 2014 at 8:44

1 Answer 1

1
$people = array();
$split = explode('|', $keypeople);
foreach ($split as $str) {
    if (preg_match('/\[\[([^]]+)\]\] \(([^)]+)\)/', $str, $match)) {
        $people[str_replace(array('[[', ']]'), '', $match[2])] = $match[1];
    }
}
var_dump($people);

The regexp matches anything with the pattern [[name]] (role).

Output:

array(3) {
  ["CEO"]=>
  string(10) "Larry Page"
  ["Chairman"]=>
  string(12) "Eric Schmidt"
  ["Director of Google X and Special Projects"]=>
  string(11) "Sergey Brin"
}
Sign up to request clarification or add additional context in comments.

7 Comments

thanks very much just just a little thing however, the CEO should be Larry Page and the Chairman should be Eric Schmidt, also is there a robust way for this to be in the case that the string $keypeople contains other roles e.g director etc?
I misunderstood the format of the string, I've fixed my code. Where in my answer have I made it only look for those two roles?
yes this works! Thanks very much. Yeah I would like to look at the entire value of a string i.e. in the case that the $keypeople contains more than just one or two values I'd like all roles and all people corresponding to those roles returned. In this case for example the string contains Sergey Brin]] (Director of [[Google X]] and Special Projects. How to adjust your code to accommodate for all roles and people?
Do you see the roles CEO or Chairman hard-coded anywhere in my answer?
Thank you! Apologies my mistake it works perfectly. Really appreciate it.
|

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.