preg-match-all

Matching Product Prices from an HTML text

半世苍凉 提交于 2019-12-02 04:28:38
I'm trying a simple regex on a string for pricing information, but my preg_match_all is simply not finding what it should. I'm looking for instance of e.g. $**.** or £**.** or sometimes the currency symbol might be encoded as an HTML entity e.g. for GBP £ or £ Is there an issue with using preg_match_all to find html entities? Here's what I'm trying: $price = preg_match_all( '#(?:\$|\£|\€|\£|\£)(\d+(?:\.\d+)?)#', $string, $matches ); But I get: Unknown modifier '1' Here is some obvious errors: 1) preg_match_all() expects at least 3 parameters, so it has to be preg_match_all( '#(?:\$|\£|\€|\£|\£

How to loop through, match and replace?

六眼飞鱼酱① 提交于 2019-12-02 03:12:04
I have multiple strings with same curly braces I want to replace them as dynamic if I get the count as 1 then need to replace the first occurrence, If count as 2 then replaces the second occurrence as so on until condition satisfies. <?php include_once("con.php"); $db = new Da(); $con = $db->con(); $String = "{{ONE}} {{TWO}} {{THREE}} {{FOUR}} {{FIVE}} {{SIX}}"; $Count = 1; if(preg_match_all("/\{\{[^{}]+\}\}/", $lclString, $matches)) { foreach ($matches[0] as $match) { $Count++; $Query = "SELECT link FROM student WHERE linkVal = '".$match."'"; $Result = $con->query($Query); if($row = $Result-

preg_replace, str_replace and substr_replace not working in special condition

六眼飞鱼酱① 提交于 2019-12-02 03:06:38
I have the following code: this code finds all html tags in a string and replaces them with [[0]], [[1]] ,[[2]] and so on.(at least that is intented but not workinng); $str = "some text <a href='/review/'>review</a> here <a class='abc' href='/about/'>link2</a> hahaha"; preg_match_all("|<[^>]+>(.*)</[^>]+>|U",$str, $out, PREG_OFFSET_CAPTURE); $count = 0; foreach($out[0] as $result) { $temp=preg_quote($result[0],'/'); $temp ="/".$temp."/"; preg_replace($temp, "[[".$count."]]", $str,1); $count++; } var_dump($str); This code finds all the tags in a string and replaces them with [[0]], [[1]] and [

Find substrings in string where substring encapsulated in specific character

落花浮王杯 提交于 2019-12-02 02:58:32
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 want... So my question is is there a way of matching only between the first and second instance, the third

Preg_match_all returning array within array?

和自甴很熟 提交于 2019-12-01 18:33:19
问题 I am trying to get the information out of this array, but for some reason it is nesting everything into $matches[0] . <? $file = shell_exec('pdf2txt.py docs/April.pdf'); preg_match_all('/.../',$file,&$matches); print_r($matches) ?> Is this working as intended? Is there a way to put this in an array of depth 1? EDIT: This is the RegEx: ([A-Z][a-z]+\s){1,5}\s?[^a-zA-Z\d\s:,.\'\"]\s?[A-Za-z+\W]+\s[\d]{1,2}\s[A-Z][a-z]+\s[\d]{4} 回答1: preg_match_all() always returns an array (if successful,

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

早过忘川 提交于 2019-12-01 18:07:00
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 $. Nambi It is a delimiter A delimiter can be any non-alphanumeric, non-backslash, non-whitespace character. 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 / '~/var/www/test~' # delimited with ~ The last one does not require to escape the / as the delimiter is now

Get all nested curly braces

早过忘川 提交于 2019-12-01 11:11:26
It is possible to get all content in nested curly braces from string? For example: The {quick} brown fox {jumps {over the} lazy} dog So i need: quick over the jumps {over the} lazy Better in this sequence, from most nested. Solution The regex below will allow you to grab the content of all the nested curly braces. Note that this assumes that the nested curly braces are balanced; otherwise, it is hard to define what the answer should be. (?=\{((?:[^{}]++|\{(?1)\})++)\}) The result will be in capturing group 1. DEMO Note that the order is not as specified in the question, though. The order

preg_match_all print *all* matches

别来无恙 提交于 2019-12-01 11:05:31
I need to print all matches using preg_match_all. $search = preg_match_all($pattern, $string, $matches); foreach ($matches as $match) { echo $match[0]; echo $match[1]; echo $match[...]; } The problem is I don't know how many matches there in my string, and even if I knew and if it was 1000 that would be pretty dumb to type all those $match[] 's. The $match[0] , $match[1] , etc., items are not the individual matches, they're the "captures". Regardless of how many matches there are, the number of entries in $matches is constant, because it's based on what you're searching for , not the results.

Get all matches from string that start with and end, using php

岁酱吖の 提交于 2019-12-01 03:27:53
How would I use PHP to extract everything in between [startstring] and [endstring] from this string: [startstring]hello = guys[endstring] hello guys [startstring]jerk = you[endstring] welcome to the universe I'd like to find all the matches of [startstring] and [endstring] , and echo the text inside. Kinda like a delimiter. And I would like all the matches echoed to be seperated by a space. How would I go about accomplishing this? Would this require regex ? Could you provide a sample? Thanks so much! :) preg_match_all('/\[startstring\](.*?)\[endstring\]/s', $input, $matches); echo implode(' ',

With PHP preg_match_all, get value of href

独自空忆成欢 提交于 2019-11-30 16:08:12
问题 I don'T really understabd how regular expressions works even after I read this tutorial http://www.webcheatsheet.com/php/regular_expressions.php Here is what I need to find: <link type="text/html" rel="alternate" href="http://link"/> And it should return: http://link Here is what I tried: $find = preg_match_all( '/<link type="text/html" rel="alternate" href=".*', $file, $patterns2 ); You can laught :) Thanks in advance for your help and your time :) 回答1: using simplexml $html = '<link type=