preg-match

How to match keys from one array to another, and return keys from the other [closed]

本秂侑毒 提交于 2021-02-17 05:56:11
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 11 months ago . Improve this question I have 2 arrays, I would like to use the keys from the 1st array to search for matched keys from the 2nd array and return those keys from the 2nd array with the values ​​of the second array. expected result: Array ( 'Intermediary/contract/{contract:id}/bank

Preg_match string inside curly braces tags

倖福魔咒の 提交于 2021-02-10 20:39:49
问题 I'd like to grab a string between tags. My tags will be with curly braces. {myTag}Here is the string{/myTag} So far I have found #<\s*?$tagname\b[^>]*>(.*?)</$tagname\b[^>]*>#s This one matches tags with angle brackets <> . I couldn't figure out how to make it look for curly braces. Eventually I would like to parse whole page and grab all matches and build an array with strings. This is the code: function everything_in_tags($string, $tagname) { $pattern = "#<\s*?$tagname\b[^>]*>(.*?)</

PHP preg_match() or strpos to check if string starts with substring

穿精又带淫゛_ 提交于 2021-02-05 11:09:43
问题 I am changing views of a page depending the $_GET['code'] it can equal a series of different codes but they will all start with either Red , Town or Bearing , then a series of characters. For example, the following would all trigger the condition RedWings-223123-NY Townmansion-2341322-KY BearingWays-23422-DC I have tried <?php if(preg_match("%(?=.*Bear)(?=.*Red)(?=.*Town)%", $_GET['code'])): ?> <span> new view </span> <?php endif ?> and <?php if( strpos($_GET['code']), "Red" ) !== false) ||

PHP - preg_match() word after other word

冷暖自知 提交于 2021-02-05 09:27:35
问题 I have a text like this one: The cat was born on 1980 and lives ... So i want to get the cat's age with regex (the text could have more than 1 occurrence of a number with 4 digits). I'm trying this preg_match('/born on [0-9]{4}/', $text, $matches) but the result is: array('born on 1980') . I want to ignore everything before a year. Demo: http://www.phpliveregex.com/p/aYl 回答1: Group the value you want then it will be the first item of the array. $text = 'The cat was born on 1980 and lives ...'

PREG_MATCH check all words and condition

落花浮王杯 提交于 2021-02-05 07:39:16
问题 I've written a regular expression which seraches the search terms in OR condition, such that provided three words have in string irrespective of their order. Now i want to just put an AND condition as I want to get ALL THREE words simulatniously in a string with different order. Here's my preg_match() regular expresison. $myPregMatch = (preg_match("/\b^(Word1|Word2|Word3)\b/", "Word4 Word2 Word1 Word3 Word5 Word7")); if ($myPregMatch){ echo "FOUND !!!"; } I want to find in string "Word4 Word2

Regex to pull out the string between 2 underscores

两盒软妹~` 提交于 2021-02-04 19:44:05
问题 I have a pattern of type anystring_OPCtarget_anystring . Can I get some help as how to verify if the string between the 2 underscores is of type "OPC(target)" and pull out the target using regex. Suppose if my string is: MP700000001_OPC32_812345643 first, I need to verify if the string between underscores starts with OPC and then get the target text after OPC and before the 2nd underscore. Help appreciated!!! Thank you 回答1: You can use this regex: ^[^_]*_OPC\K[^_]+ And grab matched data. ^[^_

PHP Regex Word Boundary exclude underscore _

白昼怎懂夜的黑 提交于 2021-02-04 15:45:36
问题 I'm using regex word boundary \b, and I'm trying to match foo in the following $sentence but the result is not what I need, the underscore is killing me, I want underscore to be word boundary just like hyphen or space: $sentence = "foo_foo_foo foo-foo_foo"; X X X YES X X Expected: $sentence = "foo_foo_foo foo-foo_foo"; YES YES YES YES YES YES My code: preg_match("/\bfoo\b/i", $sentence); 回答1: You would have to create DIY boundaries. (?:\b|_\K)foo(?=\b|_) 回答2: Does this do what you want?: preg

Split camelCase word into words with php preg_match (Regular Expression)

ⅰ亾dé卋堺 提交于 2021-01-29 20:22:13
问题 How would I go about splitting the word: oneTwoThreeFour into an array so that I can get: one Two Three Four with preg_match ? I tired this but it just gives the whole word $words = preg_match("/[a-zA-Z]*(?:[a-z][a-zA-Z]*[A-Z]|[A-Z][a-zA-Z]*[a-z])[a-zA-Z]*\b/", $string, $matches)`; 回答1: You can also use preg_match_all as: preg_match_all('/((?:^|[A-Z])[a-z]+)/',$str,$matches); Explanation: ( - Start of capturing parenthesis. (?: - Start of non-capturing parenthesis. ^ - Start anchor. | -

preg_match() returns 0 although regex testers work [duplicate]

六眼飞鱼酱① 提交于 2021-01-29 05:22:12
问题 This question already has answers here : Matching Unicode letter characters in PCRE/PHP (5 answers) Closed 9 months ago . I'm trying to validate a string using /[\p{L}\s]{6,}/ and trying to match characters only (Unicode ones as well). I used regex101 to test my regex and it works for the string Владимир Алексић . However, when I use that regex in preg_match() with the same string, it always returns 0 . Yet, it returns 1 if I avoid all characters except A-Za-z . Why is that so? 回答1: The \p

Why preg_match() returns first match at index 1 (not 0) of returned array?

混江龙づ霸主 提交于 2021-01-28 19:41:57
问题 Im working on small website scrapper with cURL. I decided to use preg_match to find header and article content. This is my code: preg_match('@<h2 class="title">(.*?)</h2>@s', $this->website, $this->title); if(sizeof($this->title) > 1) $this->title = trim($this->title[1]); // rewrite first element of array to regular variable I was experimenting with it and I found, that if there is one match - it returns it in array at index 1, not 0. Edited question: Why is this 1, not 0? Im doing something