preg-match-all

Get number before selected word form string using preg_match function in php

泪湿孤枕 提交于 2019-12-07 22:25:56
问题 I am trying to get the mm and inches values from the following string using preg_match . $string = "Gold 5.0mm Rolo Chain 18" In"; How can I do this? 回答1: Regex: ([\d\.]+)(?:mm|\") ([\d\.]+)(?:mm|\") This will match digits , . before mm and " . PHP code demo <?php ini_set('display_errors', 1); $string='Gold 5.0mm Rolo Chain 18" In'; preg_match_all("/([\d\.]+)(?:mm|\")/", $string,$matches); print_r($matches);// its 1 index will return your desired result Or: Regex: /[\d\.]+(?=mm|\")/ [\d\.]+(?

How can I extract certain HTML tags e.g. <ul> using Regex with preg_match_all in PHP?

十年热恋 提交于 2019-12-07 19:55:09
问题 I am new to regular expressions. I want to fetch some data from a web page source. I used file_get_contents("url") to get the page's HTML source. Now I want to capture a portion within some special tags. I found preg_match_all() works for this. Now I want some help to solve my problem and if possible help me to find out how to solve similar problems like this. In the example below, how would I get the data within the <ul> ? (I wish this sample HTML code could be easier for me to understand.)

Regex to match string between %

我是研究僧i 提交于 2019-12-07 16:33:08
问题 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?

preg_match_all ensure distinct/unique results

半腔热情 提交于 2019-12-07 04:40:33
I am using the following code to match all variables in a script starting with '$', however i would like the results to not contain duplicates, ie be distinct/unique: preg_match_all('/\$[a-zA-Z0-9]+/', $code, $variables); Any advice? Use array_unique to remove the duplicates from your output array: preg_match_all('/\$[a-zA-Z0-9]+/', $code, $variables); $variables = array_unique($variables[0]); But I hope you’re not trying to parse PHP with that. Use token_get_all to get the tokens of the given PHP code. Don't do that with regex. After you collected them all in your $variables , simply filter

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

本小妞迷上赌 提交于 2019-12-06 20:06:22
问题 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

preg_match_all and preg_replace in Ruby

故事扮演 提交于 2019-12-06 19:44:29
问题 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! 回答1: 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

Get number before selected word form string using preg_match function in php

只愿长相守 提交于 2019-12-06 14:01:18
I am trying to get the mm and inches values from the following string using preg_match . $string = "Gold 5.0mm Rolo Chain 18" In"; How can I do this? Regex: ([\d\.]+)(?:mm|\") ([\d\.]+)(?:mm|\") This will match digits , . before mm and " . PHP code demo <?php ini_set('display_errors', 1); $string='Gold 5.0mm Rolo Chain 18" In'; preg_match_all("/([\d\.]+)(?:mm|\")/", $string,$matches); print_r($matches);// its 1 index will return your desired result Or: Regex: /[\d\.]+(?=mm|\")/ [\d\.]+(?=mm|\") match digits , . and positively look ahead for mm and " PHP code demo <?php ini_set('display_errors'

preg_match_all to find all URL but exclude email

谁都会走 提交于 2019-12-06 10:47:18
问题 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:

How can I extract certain HTML tags e.g. <ul> using Regex with preg_match_all in PHP?

耗尽温柔 提交于 2019-12-06 09:25:34
I am new to regular expressions. I want to fetch some data from a web page source. I used file_get_contents("url") to get the page's HTML source. Now I want to capture a portion within some special tags. I found preg_match_all() works for this. Now I want some help to solve my problem and if possible help me to find out how to solve similar problems like this. In the example below, how would I get the data within the <ul> ? (I wish this sample HTML code could be easier for me to understand.) <div class="a_a">qqqqq<span>www</span> </div> <ul> <li> <div class="a_a"><h3>aaaa</h3> aaaa aaaaa</div>

Regex pattern using w.* not matching text starting with foreign characters such as Ä

╄→尐↘猪︶ㄣ 提交于 2019-12-06 01:39:07
问题 I have the following regex that I have been using successfully: preg_match_all('/(\d+)\n(\w.*)\n(\d{3}\.\d{3}\.\d{2})\n(\d.*)\n(\d.*)/', $text, $matches) However I have just found that if the text that the (\w.*) part matches starts with a foreign character such as Ä , then it doesn't match anything. Can anyone help me with what the correct pattern should be instead of (\w.*) to match a string that starts with any character? Many thanks 回答1: If you do want to match umlauts, then add the regex