PHP preg_grep not working? - php

$fileNameMatchRegex = ^a-[0-9]*_b-[0-9]*_c-.*(_d-on)?_((19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01]))-[0-9]*\.csv$
$fileNames =
Array
(
[0] => index.html
[1] => a-34234234_b-3271_c-123_d-on_2013-08-12-10.csv
[2] => a-52342345_b-3271_c-123_d-on_2013-08-12-11.csv
[3] => a-8764453_b-3271_c-123_d-on_2013-08-12-12.csv
[4] => a-7654334_b-3271_c-1234_d-on_2013-08-12-4.csv
[5] => a-3435_b-3271_c-23re_d-on_2013-08-12-5.csv
[6] => a-909876_b-3271_c-wef2r2_d-on_2013-08-12-6.csv
[7] => a-345456_b-3271_c-23rwef_d-on_2013-08-12-7.csv
[8] => a-98765_b-3271_c-23ref_d-on_2013-08-12-8.csv
[9] => a-098765_b-3271_c-wef2r_d-on_2013-08-12-9.csv
)
$matchingFileNames = preg_grep ("/".$fileNameMatchRegex."/", $fileNames);
This works here...I get a match on 1-9: regex tester

Its working fine for me
$fileNames = explode(',', "index.html,a-34234234_b-3271_c-123_d-on_2013-08-12-10.csv,a-52342345_b-3271_c-123_d-on_2013-08-12-11.csv,a-8764453_b-3271_c-123_d-on_2013-08-12-12.csv,a-7654334_b-3271_c-1234_d-on_2013-08-12-4.csv,a-3435_b-3271_c-23re_d-on_2013-08-12-5.csv,a-909876_b-3271_c-wef2r2_d-on_2013-08-12-6.csv,a-345456_b-3271_c-23rwef_d-on_2013-08-12-7.csv,a-98765_b-3271_c-23ref_d-on_2013-08-12-8.csv,a-098765_b-3271_c-wef2r_d-on_2013-08-12-9.csv");
$fileNameMatchRegex = '^a-[0-9]*_b-[0-9]*_c-.*(_d-on)?_((19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01]))-[0-9]*\.csv$';
$matchingFileNames = preg_grep ("#".$fileNameMatchRegex."#", $fileNames);
print_r($fileNames);
print_r($matchingFileNames);
but i used '#' around the regexp as delimiter, as your regexp included '/',
you proberly just need to escape your '/' or chose a better delimiter

Related

How to extract certain words from a php string?

I have a long string like this I1:1;I2:2;I8:2;NA1:5;IA1:[1,2,3,4,5];S1:asadada;SA1:[1,2,3,4,5];SA1:[1,2,3,4,5];. Now I just want to get certain words like 'I1','I2','I8','NA1' and so on i.e. words between ':'&';' only ,and store them in array. How to do that efficiently?
I have already tried using preg_split() and it works but giving me wrong output. As shown below.
// $a is the string I want to extract words from
$str = preg_split("/[;:]/", $a);
print_r($str);
The output I am getting is this
Array
(
[0] => I8
[1] => 2
[2] => I1
[3] => 1
[4] => I2
[5] => 2
[6] => I3
[7] => 2
[8] => I4
[9] => 4
[10] =>
)
Array
(
[0] => NA1
[1] => 5
[2] =>
)
Array
(
[0] => IA1
[1] => [1,2,3,4,5]
[2] =>
)
Array
(
[0] => S1
[1] => asadada
[2] =>
)
Array
(
[0] => SA1
[1] => [1,2,3,4,5]
[2] =>
)
But I am expecting 'I8','I1','I2','I3','I4' also in seperated array with position [0]. Any help on how to do this.
You could try something like.
<?php
$str = 'I1:1;I2:2;I8:2;NA1:5;IA1:[1,2,3,4,5];S1:asadada;SA1:[1,2,3,4,5];SA1:[1,2,3,4,5];';
preg_match_all('/(?:^|[;:])(\w+)/', $str, $result);
print_r($result[1]); // Matches are here in $result[1]
You can perform a greedy match to match the items between ; and : using preg_match_all()
<?php
$str = 'I1:1;I2:2;I8:2;NA1:5;IA1:[1,2,3,4,5];S1:asadada;SA1:[1,2,3,4,5];SA1:[1,2,3,4,5];';
preg_match_all('/;(.+?)\:/',$str,$matches);
print_r($matches[1]);
Live Demo: https://3v4l.org/eBsod
One possible approach is using a combination of explode() and implode(). The result is returned as a string, but you can easily put it into an array for example.
<?php
$input = "I1:1;I2:2;I8:2;NA1:5;IA1:[1,2,3,4,5];S1:asadada;SA1:[1,2,3,4,5];SA1:[1,2,3,4,5];.";
$output = array();
$array = explode(";", $input);
foreach($array as $item) {
$output[] = explode(":", $item)[0];
}
echo implode(",", $output);
?>
Output:
I1,I2,I8,NA1,IA1,S1,SA1,SA1,.

Get value of Last slash and before . in php [duplicate]

This question already has answers here:
How can I get the name of the image from url?
(8 answers)
Closed 5 years ago.
I have array like below
Array
(
[0] => C:\wamp\www\sthub\application\controllers/../../download/qr/SyR04A-94527.jpg
[1] => C:\wamp\www\sthub\application\controllers/../../download/qr/SyR05A-95528.jpg
[2] => C:\wamp\www\sthub\application\controllers/../../download/qr/SyR06A-961000001.jpg
[3] => C:\wamp\www\sthub\application\controllers/../../download/qr/SyR06A-96529.jpg
[4] => C:\wamp\www\sthub\application\controllers/../../download/qr/SyR07A-971000002.jpg
[5] => C:\wamp\www\sthub\application\controllers/../../download/qr/SyR07A-97530.jpg
[6] => C:\wamp\www\sthub\application\controllers/../../download/qr/SyR08A-981000003.jpg
[7] => C:\wamp\www\sthub\application\controllers/../../download/qr/SyR08A-98531.jpg
[8] => C:\wamp\www\sthub\application\controllers/../../download/qr/SyR09A-991000004.jpg
[9] => C:\wamp\www\sthub\application\controllers/../../download/qr/SyR09A-99532.jpg
[10] => C:\wamp\www\sthub\application\controllers/../../download/qr/SyR09A-99533.jpg
[11] => C:\wamp\www\sthub\application\controllers/../../download/qr/SyR09A-99534.jpg
[12] => C:\wamp\www\sthub\application\controllers/../../download/qr/Syno53.jpg
[13] => C:\wamp\www\sthub\application\controllers/../../download/qr/Syno54.jpg
[14] => C:\wamp\www\sthub\application\controllers/../../download/qr/Syno55.jpg
[15] => C:\wamp\www\sthub\application\controllers/../../download/qr/Syno56.jpg
[16] => C:\wamp\www\sthub\application\controllers/../../download/qr/Syno57.jpg
)
I want to get value after last slash and before .jpg like SyR04A-94527,SyR05A-95528 etc..
You're trying to parse filenames, and PHP already has plenty of built-in functions to do that. There's no need to manipulate the string itself with explode or regular expressions, etc:
$filename = 'C:\wamp\www\sthub\application\controllers/../../download/qr/SyR04A-94527.jpg';
echo pathinfo($filename, PATHINFO_FILENAME);
// SyR04A-94527
Using Regx:
<?php
$a = [
'C:\wamp\www\sthub\application\controllers/../../download/qr/SyR04A-94527.jpg',
'C:\wamp\www\sthub\application\controllers/../../download/qr/SyR05A-95528.jpg'
];
foreach( $a AS $path ){
if( preg_match('/\/([^\/]+)\.[a-z]+$/i', $path, $match))
print_r($match[1]."\n");
}
Outputs:
SyR04A-94527
SyR05A-95528
You can test it here http://sandbox.onlinephpfunctions.com/code/081543329dda8b9e0ef59836995184171ff4ce66
I was going to use pathinfo but my sandbox site has it disabled, and I'm to lazy to turn my server on. But it would be something like this:
$a = [
'C:\wamp\www\sthub\application\controllers/../../download/qr/SyR04A-94527.jpg',
'C:\wamp\www\sthub\application\controllers/../../download/qr/SyR05A-95528.jpg'
];
foreach( $a AS $path )
echo pathinfo ($path, PATHINFO_FILENAME)."\n";
Cheers!

Explode the word from sentence regardless of any special characters using PHP regular expression

I have this function to explode the word from sentence:
function explode_words($input_string='')
{
$keywords = preg_split("/[\s,]+/", $input_string); //Here REGEX
return $keywords; // as array
}
I give an example here:
$input_string = 'Pokok < getah < > sumber asli! yang bernilai . Banyak produk guna getah ?';
The exploded words will be:
Array
(
[0] => Pokok
[1] => <
[2] => getah
[3] => <
[4] => >
[5] => sumber
[6] => asli!
[7] => yang
[8] => bernilai
[9] => .
[10] => Banyak
[11] => produk
[12] => guna
[13] => getah
[14] => ?
)
How to exclude all the special characters like ~!##$%^&*()_+{}|:"<>?' ?
The word asli! should return only asli only.
Any help would be appreciated.
Thanks and have nice Monday!
You can use something like "/[^\p{L}]+/" to split the string.
function explode_words($input_string = "") {
return preg_split("/[^\p{L}]+/u", $input_string, null, PREG_SPLIT_NO_EMPTY);
}
\p{L} is a Unicode category that includes lowercase, uppercase, etc. alphabetic characters (you can look at it as an expanded version of [a-zA-Z] that includes characters from most alphabets in the world).
The [^...] part means "anything not in this group".
How about:
function explode_words($input_string='')
{
$keywords = array_filter(explode(' ', preg_replace('/[^ a-z]/i','',$input_string)));
return $keywords;
}
Remove special characters
Explode words by whitespace
Filter out empty parts of array
Result:
Array
(
[0] => Pokok
[2] => getah
[5] => sumber
[6] => asli
[7] => yang
[8] => bernilai
[10] => Banyak
[11] => produk
[12] => guna
[13] => getah
)
It may be easier to specify what you need to retain like so,
preg_split("/[^a-zA-Z]+/", $input_string, NULL, PREG_SPLIT_NO_EMPTY);
In this example, a-z and A-Z are allow ranges. You could change it as needed.

preg_match or operator

My code below produces an error, unknown modified "|"... I'm trying to use it as the OR operator. What is the correct way to run this code without error?
$p = "(\w+)|(\()|(\))|(\,)";
$s = "sum(blue,paper,green(yellow,4,toast)))";
preg_match($p,$s, $matches);
print_r($matches);
Edit
Okay I changed it a bit... ~(\w+|\(|\)|,)~
Now... here's the problem: I need to take that string and split it into an array like this:
array("sum","(","blue","paper","green","(" ... etc );
Can someone help me do that? when I run the above expression it outputs an empty array....
Thanks
You are missing the delimiter for your pattern.
$p = "~(\w+)|(\()|(\))|(\,)~";
You're missing the delimiter as #Crayon correctly mentioned, also this pattern does the same thing:
$p = '~(\w+|[(]|[)]|,)~';
As for your (new) problem, try this:
$p = '~([(),])~';
$str = 'sum(blue,paper,green(yellow,4,toast)))';
$res = preg_split($p, $str, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
print_r($res);
Output:
Array
(
[0] => sum
[1] => (
[2] => blue
[3] => ,
[4] => paper
[5] => ,
[6] => green
[7] => (
[8] => yellow
[9] => ,
[10] => 4
[11] => ,
[12] => toast
[13] => )
[14] => )
[15] => )
)

Get only internal links and external from array (RegEx and PHP)

This is my array output:
Array
(
[0] => http://www.example.com/some-page/
[1] => http://www.example.com/another-page/
[2] => http://www.example.com/third-page/
[3] => https://www.example.com/ssl/
[4] => https://www.example.com/with-slash-at-the-end/
[5] => https://www.example.com/without-slash-at-the-end
[6] => /internal-link
[7] => /anther-internal-link/
[8] => https://www.my-own-domain.com/internal-link-too-but-with-absolute-path/
)
How can I get only external and only internal links from this array? I doesn't matter which one.
This regex seems to work fine for finding internal link
(.*my-own-domain\.com.*)|(^\/.*$)
Regex Demo
PHP Code
$re = "/(?:.*my-own-domain\\.com.*)|(?:^\\/.*$)/m";
foreach($str as $x) {
if (preg_match($re, $x)) {
echo $x . "" . "\n";
}
}
Ideone Demo

Categories