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";
}
}
Related
This question already has answers here:
Explode string into array with no empty elements?
(12 answers)
Closed 2 years ago.
I want explode to return an empty array if the delimiter is not found.
Currently, I do something like this to get the explode behavior I want:
if (strpos($line, ' ') === false) {
$entries = [];
} else {
$entries = explode(' ', $line);
}
There must be a more concise way of getting this behavior without having to use preg_split. preg_split has its own behavior that is also undesirable, such as including the delimiter in the resultant array entries.
So, you have a string of space delimited data, e.g. 'foo bar'? Your rule is if there's no delimiter, meaning if the string is either a single value ('foo') or an empty string (''), then you want an empty array; otherwise you want the split delimited data?
$entries = explode(' ', $line);
if (count($entries) < 2) {
$entries = [];
}
There isn't really a sane way to make this specific condition shorter, but this removes the redundant inspection of the string and clearly states what the code is trying to do.
This question already has answers here:
Extract HTML attributes in PHP with regex [duplicate]
(2 answers)
Closed 7 years ago.
i have the following html structure :
<div data-provincia="mi" data-nazione="it" ecc...
I'm trying to take "mi" with preg_match function.
This is my code :
$pattern = '/data-provincia=*"[a-zA-Z]*"/';
preg_match($pattern,$element,$provincia);
I think that the code is right but it doesn't match with anything.
Where i'm wrong?
Thanks.
You might want to use the quantifier + (1 or more) next to the character class between brackets, and remove the first star. Also added a subtpattern for you to get exactly the part you want. Give it a try :
$pattern = '/data-provincia="([a-zA-Z]+)"/';
preg_match($pattern,$element,$provincia);
echo $provincia[1];
$element = '<div data-provincia="mi" data-nazione="it" ecc...>';
$pattern = '/<div[^>]*data-provincia=\"([^\"]+)\"[^>]*>/';
preg_match($pattern,$element,$provincia);
print_r($provincia[1]);
In addition to my comment, for this simple attribute you can use the following regex:
$regex = '/data-provincia="([^"]*)/i';
preg_match($regex,$element,$matches);
echo $matches[1];
Basically match everything except a double quote as many times as possible (or none). But please at least consider using a Parser for this task, regular expressions were not meant to deal with it.
Its working fine for me
$element = '<div data-provincia="mi" data-nazione="it"></div>';
$pattern = '/data-provincia=*"[a-zA-Z]*"/';
$matches= array();
preg_match($pattern,$element, $matches);
if (!empty($matches)) {
foreach ($matches as $eachOne) {
//code to remove unwanted
$text = trim(preg_replace('/^data-provincia\=/', '', $eachOne), '""');
echo " $eachOne; $text";
}
}
This question already has answers here:
Remove resolution string from image url in PHP
(4 answers)
Closed 9 years ago.
Regular expressions are a bit of a challenge for me. My goal is to determine whether or not a filename ends with this:
_100x200.jpg
where 100 and 200 could be any integer of any number of digits.
So what I'm looking for is a way to match these filenames:
photo_1x3.jpg
abc_100x100.jpg
a file name_50x2000.jpg
Could anybody help me out?
Thanks!
You can use this regex:
/_\d+x\d+\.jpg$/i
Using inside your code you can do:
if (preg_match('/_\d+x\d+\.jpg$/', $image, $arr)) {
// matched
var_dump($arr);
}
You can use preg_match to both check and get your dimensions:
<?php
$str = array(
'photo_1x3.jpg',
'abc_100x100.jpg',
'a file name_50x2000.jpg'
);
foreach($str as $s) {
$match = preg_match( '/_([0-9]+)x([0-9]+)/', $s, $matches);
echo $match ? 'exists' : 'doesn\'t exist';
print_r($matches);
}
?>
Demo: https://eval.in/65623
This question already has answers here:
Filter multidimensional array based on partial match of search value
(3 answers)
Closed 7 years ago.
I have an array of filenames which I need to check against a code, for example
array("120_120_435645.jpg","150_150_312312.jpg","250_250_1232327.jpg");
the string is "312312" so it would match "150_150_312312.jpg" as it contains that string.
If there are no matches at all within the search then flag the code as missing.
I tried in_array but this seems to any return true if it is an exact match, don't know if array_filter will do it wither...
Thanks for any advice...perhaps I have been staring at it too long and a coffee may help :)
$filenames = array("120_120_435645.jpg","150_150_312312.jpg","250_250_1232327.jpg");
$matches = preg_grep("/312312/", $filenames);
print_r($matches);
Output:
Array
(
[1] => 150_150_312312.jpg
)
Or, if you don't want to use regex, you can simply use strpos as suggested in this answer:
foreach ($filenames as $filename) {
if (strpos($filename,'312312') !== false) {
echo 'True';
}
}
Demo!
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...