Check array for partial match (PHP) [duplicate] - php

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!

Related

PHP: Check if string contains characters NOT in array [duplicate]

This question already has answers here:
How to check if a string contains certain characters using an array in php
(5 answers)
Closed 12 months ago.
I want to validate that a string consists of numbers or commas or semicolons:
valid: 1.234
valid: 1,234
invalid: 1.23a
invalid: 1.2_4
What works:
use str_split to create an array out of the string and use in_array with array(0,1,2,3,4,5,6,7,8,9,',',';').
But this feels circuitous to me and since I have to do this with millions of strings I want to make sure there is no more efficient way.
Question: Is there a more efficient way?
If you only need to validate a string (not sanitize or modify it), you can use regex. Try the following:
if (preg_match('/^[0-9\,\;\.]+$/', $string)) {
// ok
} else {
// not ok
}
The solution using preg_match function:
// using regexp for (numbers OR commas OR semicolons) - as you've required
function isValid($str) {
return (bool) preg_match("/^([0-9.]|,|;)+$/", $str);
}
var_dump(isValid("1.234")); // bool(true)
var_dump(isValid("1.23a")); // bool(false)
var_dump(isValid("1,234")); // bool(true)

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";
}
}

PHP - preg_match for end of filename containing variable numbers [duplicate]

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

Remove elements of one array if it is found in another [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Remove item from array if it exists in a 'disallowed words' array
I have a dynamic string that clients will send and I want to create comma delimited tags from it:
$subject = "Warmly little in before cousin as sussex and an entire set Blessing it ladyship.";
print_r($tags = explode(" ", strtolower($subject)));
And yet, I want to delete a specific group of words (such as definite articles), but I want to delete the key and value of that word if it is in the exploded array:
$definite_articles = array('the','this','then','there','from','for','to','as','and','or','is','was','be','can','could','would','isn\'t','wasn\'t', 'until','should','give','has','have','are','some','it','in','if','so','of','on','at','an','who','what','when','where','why','we','been','maybe','further');
If one of these words in the $definite_article array are in the $tags array delete the key and value of that word and the new array will have these words taken out. I will have this array be used by array_rand to have a random group of words chosen out of it. I've tried many things to achieve my result, but nothing so far. Can someone help me find a resolve to this?
You are looking for array_diff:
$subject = "Warmly little in before cousin as sussex...";
$tags = explode(" ", strtolower($subject));
$definite_articles = array('the','this','then','there','from','for','to','as');
$tags = array_diff($tags, $definite_articles);
print_r($tags);
See it in action.
Sounds like an easy job for array_diff().
array array_diff ( array $array1 , array $array2 [, array $... ] )
Compares array1 against array2 and returns the difference.
Which basically means it will return array1 after it's been stripped of all values which exist in array2.

Matching words in strings [duplicate]

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...

Categories