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
Related
This question already has answers here:
Extract a single (unsigned) integer from a string
(23 answers)
Closed 6 years ago.
$fileName = "Backup_1486874341.tar.gz"
Here is my file name. How can i get only timestamp from that file name Like 1486874341
Use simple regex in preg_match() to select target digit in file name.
$fileName = "Backup_1486874341.tar.gz";
preg_match("/_(\d+)./", $fileName, $matches);
echo $matches[1];
See result in demo
Here is the sample snippet:
<?php
$str = 'Backup_1486874341.tar.gz';
preg_match_all('!\d+!', $str, $matches);
print_r($matches[0][0]);
?>
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:
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";
}
}
This question already has answers here:
Get Last Part of URL PHP
(14 answers)
Closed 1 year ago.
If I have a URL look like this:
http://wwww.example/test1/test2/test3/
How can I retrieve string test3 from the url above?
$str = explode("/","http://wwww.example/test1/test2/test3/");
echo $str[count($str)-2];
DEMO: https://eval.in/83914
Regular expression solution.
$url = 'http://wwww.example/test1/test2/test3/';
preg_match('#.*/(.*)/$#', $url, $matches);
echo $matches[1];
Demo
Using capturing group, $:
preg_match('!/([^/]+)/[^/]*$!', 'http://wwww.example/test1/test2/test3/', $matches);
echo $matches[1];
DEMO: http://ideone.com/7EVfZa
So basename() does exactly that.
$url = 'http://www.example/test1/test2/test3/';
echo basename($url); // returns : test3;
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!