FAQ Database Discussion Community


regex in preg_match_all() doesn't work as expected

php,regex,string,preg-match-all
I have the following string: "<h2>Define Vim is the greatest</h2> word processor, good <h3>Vi</h3>!". I want to select h2 and h3 like the following structure with a regex. Expected output would be: array( 0 => <h2>Define Vim is the greatviest</h2> 1 => <h3>Vi</h3> ) So I implement my regular expression...

preg Regex: Need to capture both a keyword and an ID that follows it enclosed in braces

php,regex,preg-match-all
Suppose there is a string of text blah blah blah CONTENT{asdfkuydiusyhv} blah blah blah VALUE{jdiuvhgy54bkd8} blah blah blah I need a php regex (preg_match_all) that will capture all the keywords and values into an array, where $array = 0=> $keywords[0] = CONTENT $keywords[1] = VALUE 1=> $results[0] = asdfkuydiusyhv $results[1]...

Get a word between two patterns in preg_match_all

php,regex,preg-match-all
I am trying to get the percentage value of download from the following string. [download] Destination: Kool - Get Noticed - Apurbo-7CggL03TTl4.mp4 [download] 100% of 1.60MiB in 00:21 [download] Destination: Kool - Get Noticed - Apurbo-7CggL03TTl4.m4a [download] 100% of 164.86KiB in 00:01 For ex. only the value '100' between '[download]'...

sql parse case end expression with regular expressions preg_match_all

regex,preg-match-all
I try to parse from an oracle query the content of case-end expressions. I use the following regular expr. /((?<=case)[\w\s:\=\'\".\,()\<>-]*(?=end)){1}?/gi the sql part is: DECODE( SUM( CASE WHEN a.art_ind IN ('NORMAL', 'MVAR') THEN 1 ELSE 0 END ), 0, 1, SUM( CASE WHEN a.art_ind IN ('NORMAL', 'MVAR') THEN 1 ELSE...

How to find total number of elements using regex

php,regex,preg-replace,preg-match,preg-match-all
Here is my problem I want to find the total number of <span> and </span> in the given paragraph The point is that it might contain <span id='someid'> or <span class='someclass'> And even like this This spanner is cool The only clue is that div will have < and >....

preg_match_all returning arrays

php,preg-match,preg-match-all
I recently made a small script to catch any URL's that pass through a textarea based on a form submit. The regular expression im using is: '/([\w]+).(local|test|stage|live).site.example.com/' and if submit: <p>body</p> <p>uk2.local.site.example.net training.test.site.example.net</p> <p>www.google.com</p> <p>sd2.test.site.example.net</p> i am returned with an array that contains: 0 => array(3 0 => uk2.local.site.example.net 1...

Obtain specific data with preg_match_all

php,regex,preg-match-all
I have different texts which aren't well formatted, therefore I need a pattern which works with all of them and return some specific elements (text) from it. Let's say I have this text: "AL TEST232 KW 12*/13*/17 TEST kw16TEST123 kw 15*" and I want my preg_match_all() to return something like...

Extract cookies data in header

php,regex,cookies,http-headers,preg-match-all
I have this random string. I only need particular text inside the string such as mcnb8h1apihg9ffav1ubtgal77, and Sat, 09-May-2015 11:49:58 GMT $str = 'HTTP/1.1 302 Moved Temporarily Date: Fri, 08 May 2015 11:49:58 GMT Server: Apache/2.2.22 (Debian) X-Powered-By: PHP/5.5.23-1~dotdeb.1 Expires: Thu, 19 Nov 1981 08:52:00 GMT Cache-Control: no-store, no-cache, must-revalidate,...

Notepad++:: Find lines that NOT in a particular format

php,regex,foreach,preg-match,preg-match-all
So I have a huge text file in that format : $transation[123456]='Table9912333'; $transation[123457]='Table8123321'; $transation[123458]='Chair0123334'; Everything looks great but I want to find if there is a line that is NOT in that format inside that huge text file: so IOW... This is the format of every line: $transation[{ANY NUMBER}]='{ANY TEXT}';\r\n...

Highlight partly matched keywords

regex,preg-replace,preg-match-all
I picked up this code from stackoverflow to bold matched keywords. But it only bold exactly matched keywords. For example: $text = "iphone" and $srch_term = "iphone" -> matched and bold it $text = "iphone" and $srch_term = "iph" -> No matched (I would like it to be matched and...

How to make pattern string in preg_match_all

php,regex,preg-match,preg-match-all
How to make one pattern string for preg_match_all() function If I have multiple strings to match like "G,C,D" & "A,B,C" & "E,C,D" Currently my string is $str = "/(?<=\b)([CDEFGAB](?:b|bb|m)*(?:#|##|sus|maj|min|aug)*[\d\/]*(?:[CDEFGAB](?:b|bb‌​|m)*(?:#|##|sus|maj|min|aug)*[\d\/]*)*)(?=\s|$|(<.*>))(?! \w)/" preg_match_all($str,$lyrics , $output_array); I want to add these combinations ("G,C,D" & "A,B,C" & "E,C,D") in $str string....

Convert JSON string to array WITHOUT json_decode

php,arrays,json,preg-match-all,preg-split
I am using PHP on shared server to access external site via API that is returning JSON containing 2 levels of data (Level 1: Performer & Level 2: Category array inside performer). I want to convert this to multidimensional associative array WITHOUT USING json_decode function (it uses too much memory...

Unknown modifier while using preg_quote() in preg_match_all()

php,preg-match,preg-match-all
I am having a problem with my preg_match_all() function. I have a string e.g. $string = '<div id="header">Hello</div>' preg_match_all('/'.preg_quote('<div id="header">').'(.*?)'.preg_quote('</end>').'/s', $string, $matches); And I am looking to get the output Hello. But I only get this error: preg_match_all(): Unknown modifier 'd' in C:\xampp\htdocs\classes\Functions.php on line 13 ...

php regex extract matches where tag contain a specific word

php,regex,preg-match-all
I have the following string: <product><name>Tea</name><price>12</price></product> <product><name>black coffee</name><price>23</price></product> <product><name>cheap black-coffee</name><price>44</price></product> I would like to grab all products where "coffee" or "coffee black" occurs. I tried with the following code: preg_match_all('/<product>(.*?)(black coffee|black-coffee)(.*?)<\/product>/is', $string,...

Why does this regular expression not match the first result in php?

php,regex,preg-replace,preg-match,preg-match-all
Here is my regular expression: ❰(❮\d+[\-\d]*❯)⦓([^⦔]*)⦔❱ Here is the test text (online demo in javascript where it works fine): Nulla imperdiet ❰❮6❯⦓“Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse gravida consectetur mauris, eget ornare velit consequat vitae.⦔❱❰❮7❯⦓Morbi in quam id nulla facilisis vestibulum sit amet ornare est. Duis dolor...

add a new named key into matches of the preg_match_all

php,key,preg-match-all
My original code is: $sc = 'hello 8491241 some text 6254841 some text 568241'; preg_match_all('/[0-9]{5,10}/', $sc, $matches1); $all_matches = $matches1[0]; foreach ($all_matches as $match) { $sid = '9'; $rov['batch'] = $match; $rov['scid'] = $sid; $res[] = $rov; } print_r($res); How can i add a new named key ['type'] into $matches1...

PHP preg_match_all remove url parameters on the fly

php,regex,string,preg-match,preg-match-all
I want to fetch only the 'cleaner' version of the url without any parameters. IOW... if there is a question mark inside the url remove it and everything afterwards. Here is my current line : preg_match_all('/<a(.*?)href=("|\'|)(.*?)("|\'| )(.*?)>/s',$content,$ahref); And just to be more clear here... I'm expecting that this url (for...

preg_match a specific content

php,regex,preg-match,preg-match-all
I'm really noob about regular expressions and I need a help. I tried lots of things but couldn't get the thing I need. <img src="http://img03.imgsinemalar.com/images/afis_buyuk/m/Maske.jpg" title="Maske (1994) afişi" alt="Maske (1994) afişi" itemprop="image" width="164" height="234"/> At this code src, title, alt, width and height can have various values. However, itemprop is...

PHP Regular expression to delete a string

php,regex,curl,preg-match-all
I've a small PHP script that I use to fetch data. Probably there is a regex error here, yet I am unable to solve. The output I get is something like below. If I try $title[3][1], I get the output following output : string(30) "30charstitle" var_dump($title[3][1]); gives-> string(30) "30charstitle" var_dump($title[3][2]);...

PHP preg_match_all is not working when into function

php,function,preg-match-all
There's a set of code I've used for a while to find strings into a file. But when I put it into a function, I don't get results and I think that it's preg_match_all that is not working. I don't know how to get this fixed. Here's my code (copy/pasted...

Multiple strpos possible?

php,preg-match-all,strpos
I'm trying to see if my string contains { and : and } if so returns true, but I'm missing something here. $string = "{12345:98765}"; if(strpos($string,'{*:*}')== true) { echo "string is good"; } Ideas?...

Parse content from inside div ignoring span

php,html,preg-match-all
I am trying to get the story copy. $url = 'http://www.myfoxchicago.com/story/28987351/cook-county-jail-guards-sick-calls-tripled-on-night-of-big-fight'; //$content = file_get_content($url); $content = '<div id="WNStoryBody" class=""><span id="WNStoryDateline">CHICAGO (FOX 32 News) - </span><p>The Mayweather-Pacquiao...</p></div>'; preg_match_all('<div id=\"WNStoryBody\" class=\"\"> <span id=\"WNStoryDateline\">CHICAGO (FOX 32 News) - <\/span>(.*?)<\/div>', $content,...

How to match ID and href in an effective way using PHP?

php,string,preg-match-all
Example string <a href="#fn2" class="fn-ref" id="s9781473910270.i12"> <a href="#fn3" class="fn-ref" id="s9781473910270.i13"> <a class="ref-fn-ref" href="#fn2">↩</a> <a class="ref-fn-ref" href="#fn3">↩</a> Expected output <a href="#fn2" class="fn-ref" id="s9781473910270.i12"> <a href="#fn3" class="fn-ref" id="s9781473910270.i13"> <a class="ref-fn-ref" href="#s9781473910270.i12">↩</a> <a class="ref-fn-ref" href="#s9781473910270.i13">↩</a> To get my...

Get value between character using preg_match_all PHP

php,preg-match-all
This is my code: $str = 'auto_load[>=]'; $result = get_value($str); function get_value($s) { if (preg_match_all('/[(=|>=|<=|~|!~|~!)]/', $str, $m)) { //return here... } } I want it return: ">=" (accept the return value: "=", ">=", "<=", "~", "!~" or "~!") Somebody can help me?...

preg_match_all undefined offset error

php,regex,file-get-contents,preg-match-all
Probably there is a regex error here, but I am unable to solve it. I am getting "Undefined offset: 0" error and can't get the value of the title. Here's my code. INEEDTHISONE is the title I want to fetch. for ($j = 1; $j <= 1; $j++) { for...

php - change value to src attribute if 'cid:' are found in html document

php,html,preg-replace,preg-match-all,src
how can I tell php to look if a src attribute starts with 'cid:' and if yes, replace it with another value? <img src="cid:xx_xxxxxxxx_xxxxxxxxxxxxxxxxxxx" width="544" height="340"> <?php $new_link = "http://www.example.com'"; preg_replace("#^cid:#", $new_link); ?> ...

preg_match_all only returning first match

php,preg-match-all
I've done my homework and tested as much as possible before posting, just so it is known. As the title says my preg_match_all is not finding all matches, but if I run it the same way with phptester.net it works fine. I've read the documentation on the function, and all...

preg_match_all doesn't match all hostnames

php,preg-match-all
Ok, I'm trying to get hostnames and i'm using this regex: preg_match_all("/^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$/", 'google.com some text example.com', $matches); print_r($matches[0]); Matches should be: google.com example.com but, the output is the first match only (google.com) What i should change to get all matches please?...

How to match numbers ranging from 400 to 550 with regex?

php,regex,preg-match-all
I want to validate a numeric string that must only contain numbers ranging from 400 to 550. $x = "401"; // valid $x = "551"; // invalid I want to only match the numbers between 400 and 550. I tried the following pattern: if(preg_match_all("/[400-550]+/",$x)); But it doesn't work. What is...