Questions tagged [preg-match]

Regular expression pattern matching function for the PHP programming language

The preg_match() function in searches for a match to the regular expression given using Perl Compatible Regular Expression (PCRE) syntax. Specifically, it is for matching one value or one set of data with a given pattern.

If you require multiple matches that match your pattern, you will want to use the function

Description

int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags = 0 [, int $offset = 0 ]]] )

Basic matching

preg_match('/PHP/', 'PHP')       # Match for an unbound literal
preg_match('/^PHP/', 'PHP')      # Match literal at start of string
preg_match('/PHP$/', 'PHP')      # Match literal at end of string
preg_match('/^PHP$/', 'PHP')     # Match for exact string content
preg_match('/^$/', '')           # Match empty string

Using different regular expression delimiters

preg_match('/PHP/', 'PHP')       # / as commonly used delimiter
preg_match('@PHP@', 'PHP')       # @ as delimiter
preg_match('!PHP!', 'PHP')       # ! as delimiter 

References

5363 questions
1
vote
1 answer

PHP PCRE error preg_replace

Isis
  • 4,608
  • 12
  • 39
  • 61
1
vote
1 answer

preg_match in PHP for "&$"

I am trying to implement preg_match for string containing special characters : "&$". The code that i am using : $f = fopen($path,"r"); while($lines = fgets($f)) { if(preg_match("/\&\$/",trim($lines),$matches)) { echo "Yes
"; …
Prashant Goel
  • 471
  • 4
  • 7
1
vote
2 answers

php call certain html elements and make a condition

I'm just new with php and recently I was try to make a use of preg_match_all(). source: http//:test.com Sample - Regression 01
Liyo
  • 19
  • 2
  • 7
1
vote
1 answer

I need help forming a specific regular expression(Example Inside)

I need to build a regular expression that finds a string of this format: [name] => X\n Where X is equal to the name of something and \n is a line break. I would like to somehow return X in an array using preg_match.
JayD3e
  • 2,147
  • 3
  • 21
  • 30
1
vote
3 answers

Why is my preg_match_all not working?

I am using preg_match_all to ensure that a string follows a certain pattern. It should display 'all conditions are met' becuase the string follows the pattern, but instead, it displays 'conditions net met'. $order =…
The Codesee
  • 3,714
  • 5
  • 38
  • 78
1
vote
1 answer

regex in php only match url and content inside brackets and quotes marks

I have the following text entered on a textarea and I want to parse from this text the 3 urls and if it has [ ] get the content in a format like: url["key","value"] I've been trying to solve it through regex, but am failing using this: /(\w.+?)[((…
GabouhSk8
  • 159
  • 1
  • 13
1
vote
1 answer

preg_match pattern with slashes stored in variable

I'm having trouble with this regex. (https://regex101.com/r/vQLlyY/1) My pattern is working and is: (?<=Property: )(.*?)(?= \(Contact)|(?<=Property: )(.*?)(?= - ) You'll see in the link that the property text is extracted in both these…
Warren
  • 1,984
  • 3
  • 29
  • 60
1
vote
1 answer

What to replace eregi() with?

What is eregi() replaced with in this instance? // get value of text inbetween tags function getContentByTag($tag1, $tag2, $string) { if (eregi("$tag1(.*)$tag2", $string, $out)) { $outdata = $out[1]; } return $outdata; } This…
Auzzzy
  • 59
  • 8
1
vote
2 answers

Repeated match inside match

$regex = '/\[b\](.*?)\[\/b\]/is'; $string = '[b][b][b]string[/b][/b][/b]'; This will only match until the first [/b], so if I use this regex to convert this bbcode to HTML I will end up with this: string[/b][/b] I'm using PHP preg_replace,…
Vixxs
  • 569
  • 7
  • 21
1
vote
1 answer

RegEx - groups, need [this:andthis] from a string

I hope this is a simple question, but I'm still getting my head around groups. I have this string: this is some text [propertyFromId:34] and this is more text and I will have more like them. I need to get the content between the brackets then a…
Warren
  • 1,984
  • 3
  • 29
  • 60
1
vote
4 answers

PHP preg_match combined with checkdat

I have this piece of code in the middle of my web application. if (preg_match ("/^[-0-9]/", $strDate) == TRUE) { $dateParts = explode("-", $strDate); if (count($dateParts) === 3) { try { if…
Rashid 'Lee' Ibrahim
  • 1,357
  • 1
  • 9
  • 21
1
vote
1 answer

Using Regex with product pricing trailing currency symbol

So, still learning, regex is mind numbing stuff. But I have a working regex to preg_match in php any numbers based around product pricing that follow a currency symbol £. This may be helpful as I couldn't find a working example to consider all…
Jquestions
  • 1,650
  • 1
  • 23
  • 30
1
vote
1 answer

PHP - preg_match explanation

Im a beginner in PHP I just want to ask can someone explain to me this line of code. (preg_match('/^\w{5,}$/', $username)) Thankyou in advance. :) Your answer is so much appreciated. :)
1
vote
2 answers

PHP Regex to find pattern and wrap in anchor tags

I have a string with movie titles and release year. I want to be able to detect the Title (Year) pattern and if matched wrap it in anchor tags. Wrapping it is easy. But is it possilbe to write a regex to match this pattern if I don't know what the…
MP_Webby
  • 916
  • 1
  • 11
  • 35
1
vote
1 answer

An effective way to take the appropriate values from the string and add an associative array with the key which coincided from given string

How can I get values from string and put it into an associative array, where the key must be given wildcard string. Given template is: param1/prefix-{wildcard1}/{wildcard2}/param2 Given string is: param1/prefix-name/lastname/param2 The result…
1 2 3
99
100