sathenzar Posted December 1, 2008 Share Posted December 1, 2008 (edited) All I want to do is get every <drs:import tag> in an array. For whatever reason it seems I'm too stupid to do this. I've been trying for hours. This is the code: $f = fopen( "./c/1/tpl/sitedoc.tpl", "r" ); $contents = fread( $f, filesize("./c/1/tpl/sitedoc.tpl") ); $imports = preg_match_all( "/<drs:import(.*)\/>/s", $contents, $m ); $contents = preg_replace( "/{DRSTPL_DOC}/s", $data, $contents ); echo "ECOM1: " . $m[0][0]; fclose($f); return($contents); File (sitedoc.tpl): <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <drs:import type="javascript" file="{112908-52ecce2d-d837-4912-9bb7-2df81ca5863c}.js" /> <drs:import type="javascript" file="{112908-52ecce2d-d837-4912-9bb7-2df81ca5863c}.js" /> <drs:import type="javascript" file="{112908-52ecce2d-d837-4912-9bb7-2df81ca5863c}.js" /> </head> <body> {DRSTPL_DOC} </body> </html> It just keeps spitting out the whole page or nothing at all. I've tried $m[0][0], [0][1] [1][1], nothing works. help please? Edited December 1, 2008 by sathenzar Link to comment https://www.neowin.net/forum/topic/704934-php-reg-exp-is-driving-me-insane/ Share on other sites More sharing options...
0 Kudos Veteran Posted December 1, 2008 Veteran Share Posted December 1, 2008 Special characters such as colons in your regex need to be backslash escaped. I'm not sure if <> are special characters, check the php manual page. Link to comment https://www.neowin.net/forum/topic/704934-php-reg-exp-is-driving-me-insane/#findComment-590210996 Share on other sites More sharing options...
0 sathenzar Posted December 1, 2008 Author Share Posted December 1, 2008 I'm wondering if it has to do with it being greedy. I don't know exactly how to make it ungreedy but by default it'll consume as many characters as possible. that was it I got it :) thanks guys. Link to comment https://www.neowin.net/forum/topic/704934-php-reg-exp-is-driving-me-insane/#findComment-590211050 Share on other sites More sharing options...
0 rpgfan Posted December 2, 2008 Share Posted December 2, 2008 Nevermind. ^_^ Link to comment https://www.neowin.net/forum/topic/704934-php-reg-exp-is-driving-me-insane/#findComment-590211162 Share on other sites More sharing options...
0 Jerry Grey Member Posted December 2, 2008 Member Share Posted December 2, 2008 $imports = preg_match_all( "/<drs:import(.*)\/>/s", $contents, $m ); For the code above make (.*) to (.*?) to stop it being greedy. Link to comment https://www.neowin.net/forum/topic/704934-php-reg-exp-is-driving-me-insane/#findComment-590212040 Share on other sites More sharing options...
0 C++ Posted December 2, 2008 Share Posted December 2, 2008 (edited) All I want to do is get every <drs:import tag> in an array. For whatever reason it seems I'm too stupid to do this. I've been trying for hours.It just keeps spitting out the whole page or nothing at all. I've tried $m[0][0], [0][1] [1][1], nothing works. help please? The dot is a terrible character. There is always another way. Try this one. $imports = preg_match_all("/<drs:import[^\/]*\/>/", $contents, $m); Special characters such as colons in your regex need to be backslash escaped. I'm not sure if <> are special characters, check the php manual page. <> ain't special characters. Edited December 2, 2008 by C++ Link to comment https://www.neowin.net/forum/topic/704934-php-reg-exp-is-driving-me-insane/#findComment-590212236 Share on other sites More sharing options...
0 nvme Posted December 2, 2008 Share Posted December 2, 2008 check out regex buddy. it's an insanely helpful tool for working with regular expressions and it has a built in copy tool that will escape everything accordingly for the php preg_* functions. it also has a test feature that will on the fly show you what the regular expression is matching with a given sample data while you type the regular expression. it's not free but there is an evaluation version that could at least help you for this problem. http://www.regexbuddy.com/ Link to comment https://www.neowin.net/forum/topic/704934-php-reg-exp-is-driving-me-insane/#findComment-590212256 Share on other sites More sharing options...
Question
sathenzar
All I want to do is get every <drs:import tag> in an array. For whatever reason it seems I'm too stupid to do this. I've been trying for hours.
This is the code:
$f = fopen( "./c/1/tpl/sitedoc.tpl", "r" ); $contents = fread( $f, filesize("./c/1/tpl/sitedoc.tpl") ); $imports = preg_match_all( "/<drs:import(.*)\/>/s", $contents, $m ); $contents = preg_replace( "/{DRSTPL_DOC}/s", $data, $contents ); echo "ECOM1: " . $m[0][0]; fclose($f); return($contents);File (sitedoc.tpl):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <drs:import type="javascript" file="{112908-52ecce2d-d837-4912-9bb7-2df81ca5863c}.js" /> <drs:import type="javascript" file="{112908-52ecce2d-d837-4912-9bb7-2df81ca5863c}.js" /> <drs:import type="javascript" file="{112908-52ecce2d-d837-4912-9bb7-2df81ca5863c}.js" /> </head> <body> {DRSTPL_DOC} </body> </html>It just keeps spitting out the whole page or nothing at all. I've tried $m[0][0], [0][1] [1][1], nothing works. help please?
Edited by sathenzarLink to comment
https://www.neowin.net/forum/topic/704934-php-reg-exp-is-driving-me-insane/Share on other sites
6 answers to this question
Recommended Posts