preg-match-all

preg_match_all into simple array

我的未来我决定 提交于 2019-12-06 00:11:58
问题 I have preg_match_all function: preg_match_all('#<h2>(.*?)</h2>#is', $source, $output, PREG_SET_ORDER); It's working as intended, BUT the problem is, it preg_matches all items twice and into a huge multi dimensional array like this for example where it, as intended, preg_matched all 11 items needed, but twice and into a multidimensional array: Array ( [0] => Array ( [0] => <h2>10. <em>Cruel</em> by St. Vincent</h2> [1] => 10. <em>Cruel</em> by St. Vincent ) [1] => Array ( [0] => <h2>9. <em

Regex to match string between %

主宰稳场 提交于 2019-12-05 18:56:36
I'm trying to match substrings that are enclosed in %'s but preg_match_all seems to include several at the same time in the same line. Code looks like this: preg_match_all("/%.*%/", "%hey%_thereyou're_a%rockstar%\nyo%there%", $matches); print_r($matches); Which produces the following output. Array ( [0] => Array ( [0] => %hey%_thereyou're_a%rockstar% [1] => %there% ) ) However I'd like it to produce the following array instead: [0] => %hey% [1] => %rockstar% [2] => %there% What am I missing? Replace the " . " in your regular expression with " [^%] ": preg_match_all("/%[^%]*%/", "%hey%_thereyou

PHP 7 regex not working the same way as in 5

女生的网名这么多〃 提交于 2019-12-05 18:27:08
So I have to use php 7 and I have the following regex which works just fine in 5.5 and 5.6. ([\\'\\\"].*[\\'\\\"])([\\s]*[\\s]*=>[\\s]*[\\s])(\\[([^\\[\\]]|(?R))*[\\s]*[\\s]\\])/m when I run this in any version of 5 with preg_match_all I get the correct result. Basically I am trying to match an array in a text file of the form 'key' => [ 'val1 => 'sdsd', 'val2' => '3e3', ] The above expression selects this array. In PHP 7 (7.0.8 and 7.0.9) returns no matches what so ever. Anyone has any ideas as to why and better yet how do we port expressions to 7? Edit : The code used can be found in this

Find whole line that contains word with php regular expressions

那年仲夏 提交于 2019-12-05 08:55:52
问题 I want to search for a word "session" in a text. But I would like to retrieve the whole line in which this word appears. So far I have come up with this. $pattern="[^\\n]*session[^\\n]*"; preg_match_all($pattern,$content, $matches, PREG_OFFSET_CAPTURE); But I get an error "Unknown modifier '*'". Any ideas how to make such an regular expression? 回答1: Your regular expression is missing delimiters, hence your error: $pattern = "/[^\\n]*session[^\\n]*/"; // or, with single quotes, you don't need

preg_replace change link from href

…衆ロ難τιáo~ 提交于 2019-12-05 06:15:12
问题 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 $result = '<a href="http://host.org"><img src="./sec.png"></a> <link href="./styles.css" rel="alternate stylesheet" type="text/css" /> <script type="text/javascript" src="./style.js"></script>'; echo $result; if (!preg_match('/src="https?:\/\/"/', $result)) { $result = preg_replace('/src="(http:\/\/([^\/]+)\/)?([^"]+)"/', "src=\"http://google.com/\\3\"", $result); } echo

PHP: How do I get the string indexes of a preg_match_all?

心已入冬 提交于 2019-12-05 06:09:21
let's say I have two regexp's, /eat (apple|pear)/ /I like/ and text "I like to eat apples on a rainy day, but on sunny days, I like to eat pears." What I want is to get the following indexes with preg_match: match: 0,5 (I like) match: 10,19 (eat apples) match: 57,62 (I like) match: 67,75 (eat pears) Is there any way to get these indexes using preg_match_all without looping through the text every single time? EDIT: SOLUTION PREG_OFFSET_CAPTURE ! ghostdog74 You can try PREG_OFFSET_CAPTURE flag for preg_match() : $subject="I like to eat apples on a rainy day, but on sunny days, I like to eat

How to get text in array between all <span> tag from HTML?

谁说胖子不能爱 提交于 2019-12-05 01:36:14
I want to fetch text in array between all <span> </span> tag from HTML, I have tried with this code but it returns only one occurrence : preg_match('/<span>(.+?)<\/span>/is', $row['tbl_highlighted_icon_content'], $matches); echo $matches[1]; My HTML: <span>The wish to</span> be unfairly treated is a compromise attempt that would COMBINE attack <span>and innocen</span>ce. Who can combine the wholly incompatible, and make a unity of what can NEVER j<span>oin? Walk </span>you the gentle way, My code returns only one occurrence of span tag, but I want get all text from every span tag in HTML in

preg_match_all and preg_replace in Ruby

蓝咒 提交于 2019-12-05 01:04:46
I am transitioning from php to ruby and I am trying to figure the cognate of the php commands preg_match_all and preg_replace in ruby. Thank you so much! The equivalent in Ruby for preg_match_all is String#scan , like so: In PHP: $result = preg_match_all('/some(regex)here/i', $str, $matches); and in Ruby: result = str.scan(/some(regex)here/i) result now contains an array of matches. And the equivalent in Ruby for preg_replace is String#gsub , like so: In PHP: $result = preg_replace("some(regex)here/", "replace_str", $str); and in Ruby: result = str.gsub(/some(regex)here/, 'replace_str') result

preg_match_all to find all URL but exclude email

梦想与她 提交于 2019-12-04 17:42:44
I have searched many Stackoverflow regular expression posts but couldn't find my answer. I am using the following to find all URLs in a given $text string: $pattern = "#((http|https|ftp|ftps)://)?([a-zA-Z0-9\-]*\.)+[a-zA-Z0-9]{2,4}(/[a-zA-Z0-9=.?&-]*)?#"; (agreed there might be more precise/efficient/... but it is not the problem... yet). Now with this text input: $text = "Website: www.example.com, "; $text .= "Contact us: http://www.example.com/cu?t=contactus#anchor, "; $text .= "Email: contact@example.com"; Then a preg_match_all($pattern, $text, $matches); would return these: www.example.com

preg_match_all , get all img tag that include a string

不打扰是莪最后的温柔 提交于 2019-12-04 13:06:54
this code get all img tags preg_match_all('/<img[^>]+>/i',$a,$page); but I want get tags that their filenames includes "next.gif" or "pre.gif" for example : $page = ' <img border="0" alt="icon" src="http://www.site.com/images/man.gif" width="90" height="90"> <img border="0" alt="icon" src="http://www.site.com/images/pre.gif" width="90" height="v"> <img border="0" alt="icon" src="http://www.site.com/images/2.gif"> <img border="0" alt="icon" src="http://www.site.com/images/next.gif" width="90" height="90"> '; and I output should be like this : <img border="0" alt="icon" src="http://www.site.com