preg-match-all

PHP preg_match_all + str_replace

心已入冬 提交于 2019-12-13 03:53:22
问题 I need to find a way to replace all the <p> within all the <blockquote> before the <hr />. Here's a sample html: <p>2012/01/03</p> <blockquote> <h4>File name</h4> <p>Good Game</p> </blockquote> <blockquote><p>Laurie Ipsumam</p></blockquote> <h4>Some title</h4> <hr /> <p>Lorem Ipsum</p> <blockquote><p>Laurel Ipsucandescent</p></blockquote> Here's what I got: $pieces = explode("<hr", $theHTML, 2); $blocks = preg_match_all('/<blockquote>(.*?)<\/blockquote>/s', $pieces[0], $blockmatch); if (

Where does the case insensitive modifier go in this regular expression

此生再无相见时 提交于 2019-12-12 23:05:50
问题 In the regular expression below, where does the i modifier go so as to make the search terms $searchfor case insensitive? It seems it won't work no matter where I place it. $file = 'xml.xml'; $searchfor = '(<first>|<last>|<state>)'; header('Content-Type: text/plain'); $contents = file_get_contents($file); $regExp = '/^'.$searchfor.'(.*)$/m'; if(preg_match_all($regExp, $contents, $matches)){ echo "Found matches:\n"; echo implode("\n", $matches[0]); } else { echo "No matches found"; } Contents

regex, problem with backreference in pattern with preg_match_all

折月煮酒 提交于 2019-12-12 19:12:23
问题 i wonder what is the problem with the backreference here: preg_match_all('/__\((\'|")([^\1]+)\1/', "__('match this') . 'not this'", $matches); it is expected to match the string between __('') but actually it returns: match this') . 'not this any ideas? 回答1: Make your regex ungreedy: preg_match_all('/__((\'|")([^\1]+)\1/U', "__('match this') . 'not this'", $matches) 回答2: You can't use a backreference inside a character class because a character class matches exactly one character, and a

preg_match_all not returning expected values

倖福魔咒の 提交于 2019-12-12 19:05:55
问题 The code below uses a Wikipedia API to return data. I would like to output a string containing the Industry however cannot understand why the preg_match_all does not match and return the string related to industry - in this example of UBS, I would like to see "industry =[[Banking]], [[Financial services]]" returned. This string can be seen when using print_r to output data. I'm sure I'm misunderstanding or missing something simple. Please assist. <html> <body> <form method="post"> Search:

PHP preg_match_all how to start fething results after a number of first occurencies?

寵の児 提交于 2019-12-12 06:34:53
问题 Right now i am working on a PHP script which is fetching all occurencies from a text which have "SizeName": and here is the code for doing this: preg_match_all('/\"SizeName\":\"([0-9.]+)\"/',$str,$matches); This line of code is fething occurencies from the first time when it finds "SizeName": , but how i can make it start printing data after for example the third time when it finds "SizeName": ? Is it possible and how i can achieve it ? 回答1: Try this: preg_match_all('/(?:(?:.*?"SizeName":){2}

Hyperlink Youtube to Embed Code

假如想象 提交于 2019-12-12 06:16:50
问题 I've some troubles using Preg_replace and preg_match_all to convert a Youtube URL to embed code. Yes, I know that this topic has already touched in stackoverflow but not exactly like I want. I can get the ID from a url, without html, with that: http://(?:www\.)?youtu(?:be\.com/watch\?v=|\.be/)(\w*)(&(amp;)?[\w\?=]*)? But I've the url formatted with like this: <a href="http://www.youtube.com/watch?v=C9KAqhbIZ7o" class="comment-link">http://www.youtube.com/watch?v=C9KAqhbIZ7o</a> And I want to

PHP regexp parse HTML

半腔热情 提交于 2019-12-12 05:17:22
问题 My regexp: <([a-zA-Z0-9]+)>[\na-zA-Z0-9]*<\/\1+> my string: <div> <f> </f> </div> the result is: array(2 0 => array(1 0 => <f> </f> ) 1 => array(1 0 => f ) ) why it is capturing <f></f> , and ignoring the first <div> ? 回答1: The answer is USE A PARSER INSTEAD (sorry for my shouting ). While it is sometimes faster to use a regular expression to obtain an ID or URL string, html tags need a rather error-prone way of understanding via regex. Consider the following code, isn't that much more

Nested Parentheses to Array using regex in PHP [duplicate]

拟墨画扇 提交于 2019-12-12 05:14:44
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Regular Expression to match outer brackets I have a string of the following format: (((aaa (bbb) ccc)(ddd (eee) fff) ggg)(hhh (iii) )(jjj (kkk) lll) mmm)(nnn (ooo) ppp)(qqq (rrr) sss) It basically has 3 main parts: (((aaa (bbb) ccc)(ddd (eee) fff) ggg)(hhh (iii) )(jjj (kkk) lll) mmm) (nnn (ooo) ppp) (qqq (rrr) sss) I need the search expression to get the 3 parts in an array (ignoring any sub parentheses). Once

Count unique appearance of substring in a list of words without knowing the substr?

↘锁芯ラ 提交于 2019-12-12 04:36:27
问题 * I try to count the unique appearances of a substring inside a list of words * So check the list of words and detect if in any words there are substrings based on min characters that occur multiple times and count them. I don't know any substrings. This is a working solution where you know the substring but what if you do not know ? Theres a Minimum Character count where words are based on. Will find all the words where "Book" is a substring of the word. With below php function. Wanted

preg_match_all match multiple strings and get the values written in double quotes

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-12 04:32:02
问题 Recently I asked a question regarding preg_match_all with php, and i got the perfect solution as expected but now I have some different scenario here is my string : $html = '<p> This is my Home Page.</p><p><span style="line-height: 1.42857;"> {{ type="slider" pagename="slider-1" }} {{ type="banner" pagename="banner-1" }} {{ type="testimonial" pagename="testimonial-1" }} </span></p><p> </p>'; My Code : preg_match_all('/{{ type="(.+?)" }}/', $html, $matches, PREG_SET_ORDER); echo "<pre>"; print