preg-match-all

Pattern for check single occurrency into preg_match_all

こ雲淡風輕ζ 提交于 2019-12-11 06:01:51
问题 I'm writing a function that should retrieve all occurrences that I pass. I'm italian so I think that I could be more clear with an example. I would check if my phrase contains some fruits. Ok, so lets see my php code: $pattern='<apple|orange|pear|lemon|Goji berry>i'; $phrase="I will buy an apple to do an applepie!"; preg_match_all($pattern,$phrase,$match); the result will be an array with "apple" and "applepie". How can I search only exact occurency? Reading the manual I found: http://php.net

regex to print url from any webpage with specific word in url

淺唱寂寞╮ 提交于 2019-12-11 05:05:47
问题 i am using below code to extract url from a webpage and its working just fine but i want to filter it. it will display all urls in that page but i want only those url which consists of the word "super" $regex='|<a.*?href="(.*?)"|'; preg_match_all($regex,$result,$parts); $links=$parts[1]; foreach($links as $link){ echo $link."<br>"; } so it should echo only uls where the word super is present. for example it should ignore url http://xyz.com/abc.html but it should echo http://abc.superpower.com

preg_match_all with space and comma delimiters

可紊 提交于 2019-12-11 04:43:11
问题 I've managed to get part of the preg_match returning the values I need, but am unable to get the correct syntax to return what follows that string after a comma. Any assistance is appreciated - thanks $regex = '{loadsegment+(.*?)}'; $input = '{loadsegment 123,789}'; preg_match_all($regex, $input, $matches, PREG_SET_ORDER); Current result : $matches [0] [0] {loadsegment 123} [1] 123,789 Desired result : $matches [0] [0] {loadsegment 123,789} [1] 123 [2] 789 回答1: You need two capturing groups

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

Get all text between tags with preg_match_all() or better function?

China☆狼群 提交于 2019-12-11 02:57:08
问题 2010-June-11 <remove>2010-June-2</remove> <remove>2010-June-3</remove> 2010-June-15 2010-June-16 2010-June-17 2010-June-3 2010-June-2 2010-June-1 I'm trying to find all instances that are between the <remove> tags This is what I have: $pattern = "/<remove>(.*?)<\/remove>/"; preg_match_all($pattern, $_POST['exclude'], $matches); foreach($matches as $deselect){ foreach ($deselect as $display){ echo $display."<br />"; } } This is what it returns: 2010-June-2 2010-June-3 2010-June-2 2010-June-3

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

getting empty array results using preg_match_all for values that don't match

☆樱花仙子☆ 提交于 2019-12-11 02:24:17
问题 I am using preg_match_all to search for HashTag values in a Twitter Search response. It works as I expected except for when the search results don't have any hash values in them. For some reason my $tags array still has values and I'm not sure why. Is it because my RegEx is not correct, or is it a problem with preg_match_all? Thanks $tweet = "Microsoft Pivot got Runner-Up for Network Tech from The Wall Street Journal in 2010 Technology Innovation Awards http://bit.ly/9pCbTh"; private function

Extract Image SRC from string using preg_match_all

无人久伴 提交于 2019-12-11 01:54:04
问题 I have a string of data that is set as $content, an example of this data is as follows This is some sample data which is going to contain an image in the format <img src="http://www.randomdomain.com/randomfolder/randomimagename.jpg">. It will also contain lots of other text and maybe another image or two. I am trying to grab just the <img src="http://www.randomdomain.com/randomfolder/randomimagename.jpg"> and save it as another string for example $extracted_image I have this so far.... if(

Regex - Grab a specific word within specific tags

荒凉一梦 提交于 2019-12-11 01:45:59
问题 I don't consider myself a PHP "noob", but regular expressions are still new to me. I'm doing a CURL where I receive a list of comments. Every comment has this HTML structure: <div class="comment-text">the comment</div> What I want is simple: I want to get, from a preg_match_all, the comments that have the word "cool" in this specific DIV tag. What I have so far: preg_match_all("#<div class=\"comment-text\">\bcool\b</div>#Uis", $getcommentlist, $matchescomment); Sadly, this doesn't work. But

PHP: preg_match_all that matches inner brackets first?

两盒软妹~` 提交于 2019-12-11 00:05:20
问题 In PHP I have string with nested brackets: bar[foo[test[abc][def]]bar]foo I need a regex that matches the inner bracket-pairs first , so the order in which preg_match_all finds the matching bracket-pairs should be: [abc] [def] [test[abc][def]] [foo[test[abc][def]]bar] All texts may vary. Is this even possible with preg_match_all ? 回答1: This is not possible with regular expressions. No matter how complex your regex, it will always return the left-most match first. At best, you'd have to use