preg-match

How to make pattern string in preg_match_all

折月煮酒 提交于 2019-12-11 04:18:47
问题 How to make one pattern string for preg_match_all() function If I have multiple strings to match like "G,C,D" & "A,B,C" & "E,C,D" Currently my string is $str = "/(?<=\b)([CDEFGAB](?:b|bb|m)*(?:#|##|sus|maj|min|aug)*[\d\/]*(?:[CDEFGAB](?:b|bb‌​|m)*(?:#|##|sus|maj|min|aug)*[\d\/]*)*)(?=\s|$|(<.*>))(?! \w)/" preg_match_all($str,$lyrics , $output_array); I want to add these combinations ("G,C,D" & "A,B,C" & "E,C,D") in $str string. 回答1: NOTE this answer has been dramatically edited over time to

regex - exclude a subdomain (preg_match)

霸气de小男生 提交于 2019-12-11 04:15:45
问题 I'm want to match a domain (with preg_match) but I want to exclude a subdomain. Example I want to match all subdomains on example.org except boon.example.org : I've tried this: $test = "boon.example.org"; $test2 = "null"; if(preg_match('#(?!boon)(\w+\.)?example\.org#', $test, $output)) { $test2 = $output[2] .'example.org'; } But the output of test2 is: oon.example.org and not example.org Somebody has an answer? 回答1: If you're looking for only that exact subdomain, couldn't you just check if

Regex match everything from the last occurrence of either keyword

情到浓时终转凉″ 提交于 2019-12-11 03:55:34
问题 I'm trying to match everything from the last occurrence of either keyword (foo or bar) to the end of the string. Example (a): // I want to match ' foo do you?'; $source = 'This is foo and this is bar i like foo do you?'; $pattern = '/pattern/'; preg_match($pattern, $source, $matches); I tried the following: $pattern = '/( (foo|bar) .*)$/'; Thinking it would match the last occurrence of foo and all the following text, but it instead matches the first occurrence. print_r($matches); /* Array (

preg_replace to modify SRC and HREF urls coming from curl

僤鯓⒐⒋嵵緔 提交于 2019-12-11 03:39:18
问题 I need to replace urls in the page taken by curl and add correct link to images and links. My php curl code is: <?php function getPage($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); $result = curl_exec($ch); curl_close($ch); if (!preg_match('/src="https?:\/\/"/', $result)) $result = preg_replace('/src="(.*)"/', "src=\"http://support.prophpbb.com/\\1\

Replace and retrieve placeholder value

安稳与你 提交于 2019-12-11 03:15:50
问题 Is there any way I can replace one value and retrieve another in the same string in a more efficient way than in the code below, for example a method that combines preg_replace() and preg_match() ? $string = 'abc123'; $variable = '123'; $newString = preg_replace("/(abc)($variable)/",'$1$2xyz', $string); preg_match("/(abc)($variable)/", $string, $matches); $number = $matches[2]; 回答1: You can use a single call to preg_replace_callback() and update the value of $number in the code of the

Warning: preg_match() [function.preg-match]: Unknown modifier '(' [duplicate]

淺唱寂寞╮ 提交于 2019-12-11 03:13:16
问题 This question already has answers here : Warning: preg_replace(): Unknown modifier ']' (3 answers) Closed 5 months ago . I have this HTML tags : <div class="title">Copy me if you can</div> so I wanna use preg_match to take just "Copy me if you can" I'm using this preg pattern : $preg = "<div class=\"title\">(.+?)</div>"; So I wrote this code $match = preg_match($preg,$content); The browser outputs : Warning: preg_match() [function.preg-match]: Unknown modifier '(' What 回答1: You forgot the

PHP preg_match “AND” operator

混江龙づ霸主 提交于 2019-12-11 02:53:49
问题 I use "OR" operator "|" to match onle of the words of $name variable $name = "one five six two"; if (preg_match('/(one|two)/i', $name)) {return true;} What operator should I use with preg_match to have "AND" condition if that words is inside of $name? i.e. if (preg_match('/(two "AND" five)/i', $name)) {return true;} 回答1: If you still want to use regex, you'll need positive lookaheads: if (preg_match('/^(?=.*one)(?=.*two)/i', $name)) {return true;} It's not recommended for simple stuff (kind

php RegExp: how to use newline in expression?

与世无争的帅哥 提交于 2019-12-11 02:49:09
问题 For example it works: {<div\s+class=\"article\"><h2(.*)</div>}s If I do this way, I get nothing: {<div\s+class=\"article\"> <h2(.*) </div>}s I suspect that I should use some modifier, but I know which one from here: http://php.net/manual/en/reference.pcre.pattern.modifiers.php 回答1: That would be the /x modifier: x (PCRE_EXTENDED) If this modifier is set, whitespace data characters in the pattern are totally ignored except when escaped or inside a character class, and characters between an

preg_match name of input field inside form

杀马特。学长 韩版系。学妹 提交于 2019-12-11 02:40:49
问题 Sorry, i spent like 2 hours trying to preg_match this form <form class="form-search" method="post" action="/index.php"> <div class="form-group"> <input id="address_box" type="text" class="form-control" name="x" value="" onfocus="this.select()" /> </div> <span class="btn btn-s btn-caps"><input type="submit" value="start" /></span> </form> To: Preg_match: START = <form WHERE action CONTAIN /index.php EX: action="/index.php" or action="http://whatever.com/index.php" FIND name="[A-Za-z]{1}" END =

What is this regular expression looking for [closed]

﹥>﹥吖頭↗ 提交于 2019-12-11 02:24:05
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 5 years ago . preg_match ("/[-_^%&().A-Z0-9]/", $file) By the looks of it, I can see that the tail end is Anything A-Z and 0-9, but not sure for the beginning. Because ^ is not escaped so it could be a literal "^" or evaluated to a NOT, and same goes with ".", "(" and ")". So I am just a bit confused. Does