I have a string which looks like this:
email#domain1.com|email#domain5.com
I need only the email matching 'domain5'.
$domain_needle = 'domain5.com';
$employer_email = 'email#domain1.com|email#domain5.com';
$employer_email = explode('|', $employer_email);
How can I pick the array member which has the domain5?
Using substr_count()?
$domain_needle = 'domain5.com';
$employer_email = 'email#domain1.com|email#domain5.com';
$employer_email = explode('|', $employer_email);
foreach($employer_email as $email){
if(strpos($email, $domain_needle) !== false){
$employer_email = $email;
break;
}
}
echo $employer_email;
You could use preg_grep:
$result = preg_grep("/$domain_needle/", $employer_email);
Example, with array_shift to extract the first value from the returned array:
$domain_needle = 'domain5.com';
$employer_email = 'email#domain1.com|email#domain5.com';
$employer_email = explode('|', $employer_email);
print array_shift(preg_grep("/$domain_needle/", $employer_email));
Outputs:
email#domain5.com
Related
I need to broke a string into some vars but its order is not fixed as the exemple:
$string = "name='John';phone='555-5555';city='oakland';";
$string2 = "city='oakland';phone='555-5555';name='John';";
$string3 = "phone='555-5555';name='John';city='oakland';";
so I need to broke the strings into:
$name
$phone
$city
if the position would be fixed i could use explode and call for the array key that i need like
$brokenString = explode("'",$string);
$name = $brokenString[1];
$phone = $brokenString[3];
$city = $brokenString[5];
however how could I do it with variable position??
One way to do it with sort to make the position same always for all string variables.
<?php
$string = "name='John';phone='555-5555';city='oakland';";
$string2 = "city='oakland';phone='555-5555';name='John';";
$string3 = "phone='555-5555';name='John';city='oakland';";
$array = explode(';',$string3);
sort($array);
$array = array_filter($array); # remove the empty element
foreach($array as $value){
$split = explode('=',$value);
$result[$split[0]] = $split[1];
}
extract($result); # extract result as php variables
echo "\$city = $city; \$name = $name; \$phone = $phone";
?>
EDIT: As using extract() is generally not a good idea.You can use simple foreach() instead of extract(),
foreach($result as $k => $v) {
$$k = $v;
}
WORKING DEMO: https://3v4l.org/RB8pT
There might be a simpler method, but what I've done is created an array $stringVariables which holds the exploded strings.
This array is then looped through and strpos is used in each element in the exploded string array to see if it contains 'city', 'phone', or 'name'. Depending on which one, it's added to an array which holds either all the names, cities or phone numbers.
$stringVariables = array();
$phones = array();
$names = array();
$cities = array();
$stringVariables[] = explode(";",$string);
$stringVariables[] = explode(";",$string2);
$stringVariables[] = explode(";",$string3);
foreach($stringVariables as $stringVariable) {
foreach($stringVariable as $partOfString) {
if(strpos($partOfString, "name=") !== false) {
$names[] = $partOfString;
}else if(strpos($partOfString, "city=") !== false) {
$cities[] = $partOfString;
}else if(strpos($partOfString, "phone=") !== false) {
$phones[] = $partOfString;
}
}
}
An alternative way is to convert it into something that can be parsed as a URL string.
First part is to change the values and , from 'John', to John& using a regex ('([^']+)'; which looks for a ' up to a ' followed by a ;), then parse the result (using parse_str())...
$string = "name='John';phone='555-5555';city='oakland';";
$string = preg_replace("/'([^']+)';/","$1&", $string);
echo $string.PHP_EOL;
parse_str( $string, $values );
print_r($values);
gives the output of
name=John&phone=555-5555&city=oakland&
Array
(
[name] => John
[phone] => 555-5555
[city] => oakland
)
or just using regex's...
preg_match_all("/(\w*)?='([^']+)';/", $string, $matches);
print_r(array_combine($matches[1], $matches[2]));
I have 2 array in my code, like the shown below:
<?php
$kalimat = "I just want to search something like visual odometry, dude";
$kata = array();
$eliminasi = " \n . ,;:-()?!";
$tokenizing = strtok($kalimat, $eliminasi);
while ($tokenizing !== false) {
$kata[] = $tokenizing;
$tokenizing = strtok($eliminasi);
}
$sumkata = count($kata);
print "<pre>";
print_r($kata);
print "</pre>";
//stop list
$file = fopen("stoplist.txt","r") or die("fail to open file");
$stoplist;
$i = 0;
while($row = fgets($file)){
$data = explode(",", $row);
$stoplist[$i] = $data;
$i++;
}
fclose($file);
$count = count($stoplist);
//Cange 2 dimention array become 1 dimention
for($i=0;$i<$count;$i++){
for($j=0; $j<1; $j++){
$stopword[$i] = $stoplist[$i][$j];
}
}
//Filtering process
$hasilfilter = array_diff($kata,$stopword);
var_dump($hasilfilter);
?>
$stopword contain of some stop word like attached in http://xpo6.com/list-of-english-stop-words/
All I wanna do is: I want to check if save the element that exist in array $kata and it is not exist in array $stopword
So I want to delete all the element that exist in both array $kata and $stopword .
I read some suggestion to use array_diff , but somehow it doesn't work to me. Really need your help :( Thanks.
array_diff is what you need, you are right. Here is a simplified version of what you try to do:
<?php
// Your string $kalimat as an array of words, this already works in your example.
$kata = ['I', 'just', 'want', 'to', '...'];
// I can't test $stopword code, because I don't have your file.
// So let's say it's a array with the word 'just'
$stopword = ['just'];
// array_diff gives you what you want
var_dump(array_diff($kata,$stopword));
// It will display your array minus "just": ['I', 'want', 'to', '...']
You should also double check the value of $stopword, I can't test this part (don't have your file). If it does not work for you, I guess the problem is with this variable ($stopword)
There is a problem in your $stopword array. var_dump it to see the issue.array_diff is working correct.
Try following code I wrote to make your $stopword array right:
<?php
$kalimat = "I just want to search something like visual odometry, dude";
$kata = array();
$eliminasi = " \n . ,;:-()?!";
$tokenizing = strtok($kalimat, $eliminasi);
while ($tokenizing !== false) {
$kata[] = $tokenizing;
$tokenizing = strtok($eliminasi);
}
$sumkata = count($kata);
print "<pre>";
print_r($kata);
print "</pre>";
//stop list
$file = fopen("stoplist.txt","r") or die("fail to open file");
$stoplist;
$i = 0;
while($row = fgets($file)){
$data = explode(",", $row);
$stoplist[$i] = $data;
$i++;
}
fclose($file);
$count = count($stoplist);
//Cange 2 dimention array become 1 dimention
$stopword= call_user_func_array('array_merge', $stoplist);
$new = array();
foreach($stopword as $st){
$new[] = explode(' ', $st);
}
$new2= call_user_func_array('array_merge', $new);
foreach($new2 as &$n){
$n = trim($n);
}
$new3 = array_unique($new2);
unset($stopword,$new,$new2);
$stopword = $new3;
unset($new3);
//Filtering process
$hasilfilter = array_diff($kata,$stopword);
print "<pre>";
var_dump($hasilfilter);
print "</pre>";
?>
I hope it helps
I'm having trouble finding a solution to the following problem.
I need to take the first value of one array ($split), find it in another array ($array) and return a corresponding value into a new array ($matched), then loop until all values in $split have been matched and returned (in order) to $matched.
I've annotated my code below to explain further.
<?php
$split = explode('/', '1/2/3');
// $split can be any length and any order
// EG. $split = explode('/', '1/2/3/66/4/9');
$result = $connection->query("SELECT id, filename FROM pages");
while ($row = $result->fetch_array()) {
$new_array[$row['id']] = $row;
}
foreach($new_array as $array) {
// take first value from $split
// match value with 'id' of $array
// return corresponding 'filename'
// set as first value of $matched
$matched = //
// loop until all values from $split have been matched
// and the corresponding filename has been added to $matched.
}
$join = implode('/', $matched); // join $matched as string.
?>
Let'see:
You have list of numbers (ids),
you have map: id -> filename,
you need filenames ( imploded by slash )
so:
<?php
// $split can be any length and any order
// EG. $split = explode('/', '1/2/3/66/4/9');
$split = explode('/', '1/2/3');
// NB!
$split = array_flip( $split );
$result = $connection->query("SELECT id, filename FROM pages");
/* I need exactly map id -> filename for implode() */
while ($row = $result->fetch_assoc()) {
$new_array[$row['id']] = $row['filename'];
}
$matched = array_intersect_key($new_array, $split);
$join = implode('/', $matched); // join $matched as string.
?>
Try the following example:
// ... some code
$matched = array();
foreach ( $split AS $key ) {
if ( isset($array[$key]) ) {
$matched[] = $array[$key];
}
}
return $matched;
// ... more code
I would build a mysql query this way:
SELECT id, filename FROM pages WHERE id IN(1,2,3,66,4,9) ORDER BY FIELD(id,1,2,3,66,4,9)
And then, in php:
while($row = $result->fetch_assoc()) {
$match[] = $row;
}
do {
$pet_name = $row['pet_called'];
$ary = array($pet_name);
}while($row_pet = mysql_fetch_assoc($pet));
When called it must display the value, which can be done by the for loop etc.
echo $ary[0..x];
This is not working and how do i get to do this?
$ary = array();
while ($row = mysql_fetch_assoc($pet)) {
$ary[] = $row['pet_called'];
}
$ary[] = ... is the syntax to add an element to an array.
Don't use do...while. Nothing will be set to $row in the first iteration. This is why it doesn't work. Try just while:
$ary = array();
while($row_pet = mysql_fetch_assoc($pet))
{
$ary[] = $row_pet['pet_called'];
}
I am passing the check box value in url like selected.php?aa=2,3, and i want get the 2 and 3 value from datebase when i explode its not working
$mainclass=$_GET['aa'];
$classarray = list($class) = explode(",", $mainclass);
$classa = implode(',', $classarray);
$makefeed = mysql_query("SELECT dbid FROM studentnew WHERE dbid IN ('".$classa."')");
while ($cc = mysql_fetch_array($makefeed)) {
// code
echo $cc['name'];
}
$mainclass = $_GET['aa'];
$classarray = explode(",", $mainclass);
$classarray = array_walk($classarray, 'intval');
$classa = implode(',', $classarray);
$makefeed = mysql_query("SELECT dbid FROM studentnew WHERE dbid IN (".$classa.")");