preg-match-all

Regex - PHP Lookaround

馋奶兔 提交于 2019-12-04 10:25:51
I have a string, such as this: $foo = 'Hello __("How are you") I am __("very good thank you")' I know it's a strange string, but stay with me please :P I need a regex expression that will look for the content between __("Look for content here") and put it in an array. i.e. the regular expression would find "How are you" and "very good thank you". Try this: preg_match_all('/(?<=__\(").*?(?="\))/s', $foo, $matches); print_r($matches); which means: (?<= # start positive look behind __\(" # match the characters '__("' ) # end positive look behind .*? # match any character and repeat it zero or

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

拜拜、爱过 提交于 2019-12-04 05:39:51
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 If you do want to match umlauts, then add the regex /u modifier, or use \pL in place of \w . That will allow the regex to match letters outside of the ASCII

preg_match_all into simple array

纵然是瞬间 提交于 2019-12-04 05:09:48
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>Robot Rock</em> by Daft Punk</h2> [1] => 9. <em>Robot Rock</em> by Daft Punk ) [2] => Array ( [0] => <h2

What does the “~” character signify in PHP regex?

孤街浪徒 提交于 2019-12-04 03:28:37
问题 What does the "~" character mean in the following?: preg_match_all("~<img [^>]+>~", $inputw, $output); My guess is that they are beginning and end markers such as ^ and $. 回答1: It is a delimiter A delimiter can be any non-alphanumeric, non-backslash, non-whitespace character. 回答2: As Nambi said you are free to choose the delimiter in a regex. However if the delimiter appears in the pattern it has to escaped. Knowing this, imagine the following situation '/\/var\/www\/test/' # delimited with /

preg_replace change link from href

老子叫甜甜 提交于 2019-12-03 21:41:47
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 $result; if (!preg_match('/href="https?:\/\/"/', $result)) { $result = preg_replace('/href="(http:\/\/([^\/]

getting emails out of string - regex syntax + preg_match_all [closed]

狂风中的少年 提交于 2019-12-03 12:39:04
问题 This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 6 years ago . i'm trying to get emails out of a string. $string = "bla bla pickachu@domain.com MIME-Version: balbasur@domain.com bla bla bla"; $matches = array();

preg_match_all - regex to find full urls in string

人盡茶涼 提交于 2019-12-03 08:44:00
I have spent over 4 hours trying to find a regex patter to my php code without luck. I have a string with html code. It has lot of urls formats like: site*com http://site*com http://www*site*com http://site*com/some.php http://site*om/some.php?var1=1 http://site*com/some.php?var1=1&var2=2 etc. I have the following php code working in part: preg_match_all('/\b(?:(?:https?|ftp|file):\/\/|www\.|ftp\.)[-A-Z0-9+&@#\/%=~_|$?!:,.]*[A-Z0-9+&@#\/%=~_|$]/i', $content, $result, PREG_PATTERN_ORDER); The only thing I need is ALSO capture urls with multiple query strings using "&" I get them, but not in

Getting the first image in string with php

拈花ヽ惹草 提交于 2019-12-03 04:40:48
问题 I'm trying to get the first image from each of my posts. This code below works great if I only have one image. But if I have more then one it gives me an image but not always the first. I really only want the first image. A lot of times the second image is a next button $texthtml = 'Who is Sara Bareilles on Sing Off<br> <img alt="Sara" title="Sara" src="475993565.jpg"/><br> <img alt="Sara" title="Sara two" src="475993434343434.jpg"/><br>'; preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i',

php using curl and preg_match_all

这一生的挚爱 提交于 2019-12-03 00:22:09
问题 So what I'm wanting to do is use preg_match_all to pull the number from the table below. I've tried playing around with a few regular expressions, but I'm not getting it yet. I would like to pull the numbers and print them. ie. //gets the site $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://site.org'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $response = curl_exec($ch); //parse the data preg_match_all('/[0-9]+(?=[^0-9]+(N7:0<|N7:10<|N7:20))/', $response, $matches); //prints the

Regular expression to find all “src” attribute of HTML “img” element only folder in PHP

大憨熊 提交于 2019-12-02 23:17:34
问题 I have a string, inside of that I have an image: "<img src="img/programacao/51.jpg" style="width:200px;" /><p>balbalba</p><img src="img/programacao/46.jpg" style="width:200px;" /><p>balbalba</p><img src="/img/finalCinerio.jpg"><p>balbalba</p><img src="img/topo.jpg" />" I just need the folder of images :img/programacao/ But my regex not working: preg_match_all('/<img.*?src="([^"]*)"[^>]*>(?:<img>)?/', $text, $image); Output ... [0] => img/topo.jpg [1] => img/p_veneza.png [2] => img/programacao