preg-match-all

extract img tag from a html code string through preg_match_all PHP function

为君一笑 提交于 2019-12-24 00:26:55
问题 I've some html code and extracted the img src attribute from it. Into the html string there are some img like this: <img src="http://www.pecso.it/wp-content/uploads/2016/12/10_WRAS.png"> I've tried to do this with the following PHP code: $description = wpautop($this->data->description); $description = preg_replace("/\[[^\]]+\]/", '', $description); if (preg_match_all("<img src=(.*?)>", $description, $match)) { echo match; }; and the result is NULL. Can you help me, please? 回答1: Do not use

preg_match_all ensure distinct/unique results

為{幸葍}努か 提交于 2019-12-23 04:00:22
问题 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? 回答1: 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

PCRE: Lazy and Greedy at the same time (Possessive Quantifiers)

旧时模样 提交于 2019-12-23 03:24:19
问题 I am trying to match a series of text strings with PCRE on PHP, and am having trouble getting all the matches in between the first and second. If anyone wonders why on Earth I would want to do this, it's because of Doc Comments. Oh, how I wish Zend would make native/plugin functions to read Doc Comments from a PHP file... The following example (plain) text will be used for the problem. It will always be pure PHP code, with only one opening tag at the beginning of the file, no closing. You can

Regex to find html div class content and data-attr? (preg_match_all)

ぃ、小莉子 提交于 2019-12-23 02:29:31
问题 With preg_match_all I want to get class and data-attributes in html. The example below works, but it only returns class names or only data-id content. I want the example pattern to find both class and data-id content. Which regex pattern should I use? Html contents: <!-- I want to: $matches[1] == test_class | $matches[2] == null --> <div class="test_class"> <!-- I want to: $matches[1] == test_class | $matches[2] == 1 --> <div class="test_class" data-id="1"> <!-- I want to: $matches[1] == test

Regular expression to match block of HTML

一曲冷凌霜 提交于 2019-12-23 00:49:12
问题 First I'll show you a sample of the code I'm working with: <div class="entry"> <p>Any HTML content could go here!</p> </div> </div><!--/post --> Normally I'd use a regex rule such as the following to look for a prefix and a suffix and grab everything in between: (?<=<div class="entry">).*(?=</div><!--/post -->) However, that doesnt appear to be working as it seems to be pulling the white space in between then following parts instead of the HTML content itself: <div class="entry"> <p> Any help

Grab/download images from multiple pages using php preg_match_all & cURL

放肆的年华 提交于 2019-12-21 02:36:07
问题 So I'm trying to grab some images from another site, the problem is each image is on a different page IE: id/1, id/2, id/3 etc etc so far I have the code below which can grab an image from the single URL given using: $returned_content = get_data('http://somedomain.com/id/1/'); but need to make the line above become an array (I guess) so it will grab the image from page 1 then go on to grab the next image on page 2 then page 3 etc etc automatically function get_data($url){ $ch = curl_init();

Regex to match a word, even is there are spaces between letters

喜夏-厌秋 提交于 2019-12-20 07:47:56
问题 I'd like to have a regex to match a word, even if there are spaces between the characters. When I want to match the word test , it should match the following: test t est t e s t And so on, but it should not match things like this: tste te ts s tet I have this regex: (t[\s]*e[\s]*s[\s]*t[\s]*) But I don't believe that this one is very efficient. 回答1: Actually, it is the same as t\s*e\s*s\s*t (if the word appears inside a larger string, \bt\s*e\s*s\s*t\b is preferable). This is the only way to

php using curl and preg_match_all [duplicate]

穿精又带淫゛_ 提交于 2019-12-20 07:35:24
问题 This question already has an answer here : php using curl and preg_match_all (1 answer) Closed 5 years ago . I feel like I am missing something. I am using the following code to pull some numbers from a table. As simple as it looks, I cannot seem to get anything to print. I am placing my code and an example of the table below. Please help me find my error. I want it to print out only the numbers in each cell. //gets the site $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://site.org');

Find substrings in string where substring encapsulated in specific character

六眼飞鱼酱① 提交于 2019-12-20 04:31:56
问题 I have a string in the format: "The quick __grey__ fox jumps over the lazy __brown__ dog." And I want to find and replace any words (or sometimes sentences) between the double underscores. I am currently using preg_match_all in PHP: $pattern = '/__(.*)__/'; This works fine... until it finds two sets of double underscores on the same line, such as in the above example, where it matches "__grey__" and "__brown__" as I want, but also "__grey__ fox jumps over the lazy brown__", which I do not

Regex with possible empty matches and multi-line match

假如想象 提交于 2019-12-20 03:54:15
问题 I've been trying to "parse" some data using a regex, and I feel as if I'm close, but I just can't seem to bring it all home. The data that needs parsing generally looks like this: <param>: <value>\n . The number of params can vary, just as the value can. Still, here's an example: FooID: 123456 Name: Chuck When: 01/02/2013 01:23:45 InternalID: 789654 User Message: Hello, this is nillable, but can be quite long. Text can be spread out over many lines And can start with any number of \n's. It