Matching words in strings [duplicate] - php

This question already has answers here:
Php compare strings and return common values
(3 answers)
Closed 8 years ago.
I have two strings of keywords
$keystring1 = "tech,php,radio,love";
$keystring2 = "Mtn,huntung,php,tv,tech";
How do i do return keywords that common in both strings

You can do this:
$common = array_intersect(explode(",", $keystring1), explode(",", $keystring2));
If you want them back into strings, you can just implode it back.

Hmm, interesting question... You can use this.
$arr1 = explode(',',$keystring1);
$arr2 = explode(',',$keystring2);
$duplicates = array_intersect($arr1,$arr2);
foreach($duplicates as $word) {
echo $word;
}

You could explode() both strings on commas into arrays and loop through the first array checking to see if any of the words exist in the second array using the in_array() function. If so then add that word to a "common words" array.

Those are going to need to be arrays not variables.
$keystring1 = array('tech','php','radio','love');
$keystring2 = array('mtn','huntung','php','tv','tech');
First of all...

Related

$str = "1,2,3,4,5,6,7"; How to get the sum of values? [duplicate]

This question already has answers here:
how to do a sum on a string in php?
(5 answers)
Closed 8 months ago.
I was asked this question in a PHP test,
Question: How to get the sum of values?
$str = "1,2,3,4,5,6,7";
My Solution was:
// Splitting numbers in array and adding them up
$str = "1,2,3,4,5,6,7";
$num_array = explode(',', $str);
$str = 0;
foreach($num_array as $num) {
$str+=$num;
}
echo $str;
I was told that my solution is BAD, is it BAD? Can anyone please tell why is it BAD? And any better/best solutions?
Thanks, in advance
Well, your solution is correct, but they might be expecting optimized or efficient or smallest code. May be like this:
$str = "1,2,3,4,5,6,7";
echo array_sum(explode(',', $str));
you should use array_sum(explode(",",$yourarray)) after exploding the string rather than looping array . this would be more efficient.

How to check explode contain only comma separated values...? [duplicate]

This question already has answers here:
Check if String has only Integers separated by comma in PHP
(3 answers)
Closed 6 years ago.
How to check that php explode hold only comma separated integer values.
for example i have a
$str= '10,20,30,40,50,60,70,80,90,100';
$arr = explode(',',$str);
and i want to check if the value in $str is 10,20,30 and so on. and cannot contain any of string and words.
I basically populate a drop-down from this $arr and i want to make check if user enter integer values with comma then its show the dropdown else if user enter any other format.
i also use is_int or is_numeric but it cannot solve my problem.
thanks for advance .
What you're looking for is ctype_digit
$str= '10,20,30,40,50,60,70,80,90,100';
$arr = explode(',',$str);
foreach($arr as $a){
if(!ctype_digit($a)){
//Not an int
}else{
//is an int
}
}
Output:- https://eval.in/734431
Reference:- http://php.net/ctype_digit

php parsing a basic string contained in a variable [duplicate]

This question already has answers here:
Split a comma-delimited string into an array?
(8 answers)
Closed 7 years ago.
I'm new to php and have a basic question regarding parsing strings.
I have a variable "SKU" whose value is "9897_BLK"
I need to split this into two separate values:
"STYLE" with a value of "9897" AND "COLOR" with a value of "BLK"
I suppose there's a way to use the underscore to delimit the string.
Thanks for your help.
Try explode. This basically separates your string using a delimiter as your first parameter, and the string as the second parameter and returns an array of strings generated. Afterwards you can check if the string was properly parsed or you can just directly assign values just like the one below:
$sku = "9897_BLK";
$sku_parsed = explode("_", $sku);
$style = $sku_parsed[0];
$color = $sku_parsed[1];
If you want more details, the PHP manual is very accessible and has in-depth examples and use-cases for various scenario.
http://php.net/manual/en/function.explode.php
Try this:
$sku = "9897_BLK";
list($style, $color) = explode("_", trim($sku));
In PHP we use an function to split any string with delimiter.
You may try the explode function. The explode function return an array as output and the array values are the delimited values respectively.
Here is code snippet:
$SKU = "9897_BLK";
$DELIMITED_ARRAY = explode("_", $SKU);
$STYLE = $DELIMITED_ARRAY[0];
$COLOR = $DELIMITED_ARRAY[1];

partial match in a PHP in_array() [duplicate]

This question already has answers here:
Search in array with relevance
(5 answers)
Closed 9 years ago.
I am trying to search an array for a list of words(areas).
But sometime the word(area) in the array is 2 words.
i.e in the array is "Milton Keynes" so "Milton" is not being matched
Is there any way i can do this, without splitting any double words in the array (as i assume this will be a big load on the server)
Below is an example of what i am doing
foreach (preg_split("/(\s)|(\/)|(\W)/", $words) as $word){
if (in_array($word, $areaArray)){
$AreaID[] = array_search($word, $areaArray);
}
}
Grateful, as always for any advice!
You could use preg_grep():
$re = sprintf('/\b%s\b/', preg_quote($search, '/'));
// ...
if (preg_grep($re, $areaArray)) {
// we have a match
}
You can opt to make the match case insensitive by adding the /i modifier.
You can use regular expression to find a value, this will work similar to MySQL like function
$search='Milton Keynes';
foreach ($areaArray as $key => $value) {
if (preg_match('~'.preg_quote($search).'~i',$value)) {
echo "$key";
}
}

Split string into into array of character pairs [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
PHP preg_split string into letter pairs
I have an string looking like this:
$str = "How are you doing?";
How can I turn this string into an array looking like this:
$arr = array("Ho","w ","ar","r ","yo","u ","do","in","g?");
$array = str_split($str, 2);
Documentation.
Use str_split() function.
Take a look at str_split(); it allows you to split a string by a definable amount of characters, so your code would look like:
$arr = str_split($str, 2);
Which will split $str into an array $arr where each element contains two characters,

Categories