Questions tagged [preg-match]
Regular expression pattern matching function for the PHP programming language
5,338
questions
-2
votes
1
answer
25
views
How to parse linux stat command output with regex?
I'm struggling a couple of hours already. I can't do regex and never could.
I need to capture the file name, size, type, user, group, access, modified and created with preg_match (PHP) for later usage....
1
vote
3
answers
54
views
How to write a regular expression for 5 scenarios of Date/Time?
I am looking for a solution to regular expression that would take/accept 5 variations of Date/Time
01/01/2022
12:12:00
Time: 12:12:00
Date: 01/01/2022
Date/Time: 01/01/2022 12:12:00
So far all I ...
0
votes
0
answers
28
views
php: Strip ANY URL or Domain's from String [duplicate]
I have created a Shoutbox for my users. I am trying to prevent the sharing of links.
I found a way to detect a link if the message contains actual URL's, meaning if it starts with https:// or http:// ...
1
vote
3
answers
46
views
Preg match html language with lang attribute php
I would like to use preg_match in PHP to parse the site lang out of a html document;
My preg_match:
$sitelang = preg_match('!<html lang="(.*?)">!i', $result, $matches) ? $matches[1] : '...
-2
votes
0
answers
20
views
How can I make Preg_match() accept alphanumeric string with a space? [duplicate]
I am trying to capture the full name of users from one input field. I am trying to set up my code such that the input field should only accept alphanumeric characters.
I found this regex on the ...
-1
votes
0
answers
14
views
How to "find the string characters after 2nd / (Slash)" in Regex [duplicate]
Let's say I have a string variable like this:
$string = "https://sitename.com/product-category";
preg_match("/FIND ANYTHING COMES AFTER THIRD SLASH/i", $string, $matches);
...
-3
votes
1
answer
26
views
Password Validation with preg + PHP
To avoid SQL injection, I would like to do a password validation, what's the best way, not to write quotes or the equal sign?
Do I need to block writing single or double quotes and the equal sign? ( ' ...
0
votes
1
answer
27
views
Regex in preg_match statement
Need help to fix the regex in the preg_match statement on line 13.
This is for a comment field on the form and I want the comment to be able to be as long as needed, but they should only be able to ...
1
vote
1
answer
38
views
PHP preg_match not working as Input pattern
So I have this input pattern in HTML:
<input name="firstnamereg" type="text" pattern="[/\p{L}+/u ]+">
But When I use the preg_match it does not work:
$...
0
votes
0
answers
21
views
How is it better to test return value of preg_match in PHP? [duplicate]
I'm new in PHP. I'm writing a function in PHP that uses preg_match. The function works well, but reading the official documentation on
https://www.php.net/manual/en/function.preg-match.php I have a ...
-1
votes
2
answers
86
views
How do I match this pattern using preg_match in PHP?
I'm writing a simple quiz engine in PHP and supply the question text in this format
question|correct/feedback|wrong/feedback|wrong/feedback
There can be as many wrong/feedback options as necessary. I ...
0
votes
3
answers
49
views
Moving parts of {token} to the end of line based on condition
From a multiline input I want to move tokens if they're inside {} and match some number.
Sample input
# (811) (1485) [2756] {29} [555] {15}
# (811) (1476) {20} {15} (1485) [196] [2441]
# (911) (619) ...
-2
votes
1
answer
47
views
extracting only image src, not other 'src' tags in html with php
I've been able to use preg_match on getting the src of any image tags, but I only really need the src of images with class 'wp-post-image' in this case. However, this code is returning nothing for me
$...
1
vote
1
answer
46
views
How to write preg_match for a date followed by specific string?
I want to extract date from several HTML documents. The date always follow this pattern:
Starting three alphabets of month with first character in uppercase
i-e Jan.
Two digit numerical characters of ...
0
votes
1
answer
23
views
preg_match PHP pattern search for tokens
Actually I am searching a string for specific tokens which follow a pattern @@text@@ and want to retrieve the tokens from that string into a new array:
my address is @@address@@ and my house is at @@...
0
votes
2
answers
125
views
WooCommerce phone number field validation not working if the field contains only one "0" character
I wrote a regular expression that validates the phone number in WooCommerce. It works in all cases, unless the field contains a single "0" character.
What could be the error?
That's the code:...
0
votes
0
answers
57
views
WordPress: Highlight search result exact matches
found this neat code to highlight search terms. My problem with the code is that it does not prioritize exact matches and it highlights words within other words.
So searching for "in my hometown&...
0
votes
2
answers
47
views
How can I convert from a{a{} b{}} to a[a[],b[]] using php preg_match?
Group test
{
Group test1
{
#-# Type Val
1 typ1 10
2 typ2 10
3 typ3 10
}
Group test2
{
#-# Type Val
1 ...
-1
votes
1
answer
32
views
Replacing Heading <hn> without class Tags with <p>
I wish to replace hn tags that does not contain class attribute. The idea is to match anything that follow hn except a string contain a class="something" + and ends with >
This is my ...
0
votes
1
answer
37
views
Validating form input with preg_match() returns 0 every time
I am validating the form input for a form field labeled 'tags', where users input tags that describe the uploaded image. After this validation, I preg_replace() any spaces with commas such that this ...
-1
votes
2
answers
35
views
preg_match unconditionally return key for unmatched optional capture group
$pattern = "/^(?<animal>DOG|CAT)?(?<color>BLUE|RED)?$/i";
$str = "DOG";
preg_match($pattern, $str, $matches);
$matches = array_filter($matches, 'is_string', ...
0
votes
2
answers
104
views
Word with special characters match via preg_match in php
Suppose I have a string like:
$str ="Model number is FM223-56/89.";
And I want match the model number only in this string via preg_match.
I am doing it like:
$model ="FM223-56/89."...
0
votes
1
answer
39
views
preg_match $matches does not return special characters
I have a placeholder in my content which has the following format
{{label1#label2_label3}}}
And I'm correctly matching it with this regex
preg_match('/\{\{(\w+|d+|_+|#+)*\}\}/i', $content, $matches);
...
1
vote
1
answer
38
views
preg_match to find weird words by spammer
anyone can help me dealing with a spammer that keeps finding ways to go around my spam filter?
Basically his domain name is something like abcdef.com and he uses stuff like:
a#b#c#d#e#f.com
a b c d e ...
1
vote
1
answer
92
views
Extract data with regex (with optional string before the match)
I have an array of strings. I am trying to extract the data in the parentheses, ( and ), from each string. The problem is that it does not extract the data in the middle from the first element, if ...
1
vote
1
answer
62
views
preg_match is faster than strpos on large text?
I am currently updating very old script written for PHP 5.2.17 for PHP 8.1.2. There is a lot of text processing code blocks and almost all of them are preg_match/preg_match_all. I used to know, that ...
1
vote
1
answer
39
views
using preg_match to find all values in brackets and pass them to variable
this is my first time posting.
I have a chat for a browser game, where people can send strings like this one:
Valentina stars a fight with Paul. She is using a [sword], a [shield], and a [dagger].
Now,...
0
votes
1
answer
30
views
How to match all @__('...') on string in php regex?
I have the below string and need to match all @__('...') or @__("...") in a string.
$string = <<<EOD
@__('test1') @__('test2 (foo)') @__('test\'3') @__("test4")
@...
1
vote
1
answer
109
views
Extracting video id from youtube url with regex works hardcoded but not from input string
I have a text input field for a user to input a video url, for example:
https://www.youtube.com/watch?v=d4bv2NaO9fQ (this is just a video I selected at random to test)
I then want to extract the id ...
-2
votes
1
answer
163
views
Regex that accepts only three specific letters, a hyphen , 6 or 9, then numbers
It must start with SEQ or ABC or MHT followed by '-' then 6 or 9 then followed by any number of digits.
example: SEQ-900000 (should pass)
$value = $data['firstname'];
// ^$ = anchors, [a-zA-Z ] = ...
1
vote
2
answers
117
views
PHP: Check for characters in the Latin script plus spaces and numbers
I am new to regex and I have been going round and round on this problem.
PHP: Check alphabetic characters from any latin-based language? gives the brilliant regex to check for any characters in the ...
0
votes
0
answers
31
views
Add new values to an array if preg_match found match recursively with php
how to add a new value to a multidimensional array keeping the key if preg_match found a match with the files in my directory ?
<?php
if(isset($_POST['submit'])){
$txt1 = $_FILES['filetxt']['...
1
vote
1
answer
49
views
what's the best way to keep passing the pattern when you encounter a character like "á" with regex
for example I would liked to take the number 1, in this example but the question is how to by pass "Páginas" or 'á', 'é', 'ó'...
example in regex.com
I too would like to see a complete ...
-2
votes
1
answer
25
views
Get two time from a sentence in a string [closed]
I have a string with a sentence that has two hh:mm format time and i need to get only the time i tried with preg_match but with no success. The problem is that in the string is also a standard number ...
1
vote
1
answer
64
views
PHP parse Ping command output with preg_match
I would like to parse the output of the ping command in Linux.
My current code is working fine, the only problem is, that it only parse the first result.
I would like to change it to parse every line.
...
-2
votes
2
answers
84
views
PHP preg_match with file_get_contents empty array match
I try to extract data with preg_match
<?php
$HTML = '#a#TEXT#a#';
$REGEX = '/#a#(.*?)#a#/';
preg_match($REGEX, $HTML, $MATCH);
print_r($MATCH);
var_dump($MATCH);
?>
This works fine.
But if ...
1
vote
3
answers
152
views
Multiline to associative array with PHP
I have a block of text in a variable $s:
FOO:317263
BAR:abcd
BAZ:s fsiu sfd sdf s dfsdddddd
What's the easiest way (and with good parsing performance) to get an associative array:
$a = array('FOO' =&...
0
votes
1
answer
53
views
PHP Preg_match for authorized characters
i'm a noob in regular expressions.
Il would like to prevent a form for special characters.
The characters auhorized are :
^#{}()<>|_æ+@%.,?:;"~\/=*$£€!
I made a preg_match rule that makes ...
0
votes
1
answer
42
views
How to detect and get less or many (.*) syntax in string
I have a syntax {deck:name}(main deck)(extra deck)(side deck)(side skills)
Example I have a long content with (main deck)(extra deck)(side deck)(side skills) and (main deck)(extra deck)(side deck) and ...
1
vote
1
answer
34
views
preg_match string from meta name
I want to preg_match number "20,956" from $url1 and number "2,894,865" from $url2
$url1:
<meta name="description" content="‎ST. Eye Clinic - &#...
-2
votes
1
answer
114
views
Validate multiline text so that all lines contain text with a strict, pipe-delimited format
Let's say we have an input that may consist of one or more lines of text and all lines must obey a strict format.
number|any character(s)|number
Ex.:
150|example.com/|2500
600|hello_world|1500
The ...
0
votes
0
answers
25
views
Convert to preg_match [duplicate]
I have upgraded my php to php7 and encountered Fatal error: Uncaught Error: Call to undefined function eregi() at line 20
eregi('<State try="'.$val.'">(.*)</State>', $line, $out);...
0
votes
2
answers
94
views
Check if name ends by string in PHP
In my folder cache, i have dozens of files whose name is filename-number.json, like
sifriugh-80.json
dlifjbhvzique-76.json
dfhgzeiuy-12.json
...
I have a simple script that cleans my cache dir every ...
0
votes
0
answers
37
views
Place or add character(s) on preg_match
I got f.e. a string
foo-bar/baz 123
and a pattern
#foo-(bar/baz)#
This pattern would give me the captured bar/baz,
But i would like to replace (sanitize) the / with a - to receive bar-baz.
Reason: ...
-1
votes
1
answer
61
views
php preg_match_all get second select box options [duplicate]
I am applying preg_match_all on a webpage select box that i get from file_get_contents but the problem is that it is only getting first select box and is not able to get second select box and it's ...
0
votes
1
answer
29
views
regex for 2 numbers and a sign
I need a preg_match with regex for control a number input.
User has to enter a number between 1-80.
But after 1-80 so the 3rd input can be a + sign or ^.
So 1-80 is possible, but 79+ or 61^ has to be ...
-1
votes
3
answers
101
views
extract decimal numbers from string
I would like to extract decimal numbers (at least with 2 decimal places)
I tried this:
$myString = 'Rechnung WEEE-Reg.-Nr. DE 89633988, Batterie-Reg.-Nr. 21003868 LU-BIO-04 Amazon EU S.à r.l. - 38 ...
0
votes
0
answers
62
views
PHP compare strings in loop with similar_text, but only show if match is unique
I have an array with movie titles. I want to match those movie titles with movies I have in a database. Furthermore, I do the comparison with PHP's similar_text() function. That works fine so far, but ...
-1
votes
2
answers
83
views
Preg_Match isn't working. Error: "The requested URL was not found on this server. "
I have try different ways checking if usernames are valid in my site, but its still not detected by preg_match.
After I clicked sign up button its showed "invalidusername" on browser:
[1]: ...