• 0

PHP reg. exp is driving me insane


Question

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( "/&lt;drs:import(.*)\/&gt;/s", $contents, $m );
		$contents = preg_replace( "/{DRSTPL_DOC}/s", $data, $contents );
		echo "ECOM1: " . $m[0][0];
		fclose($f);
		return($contents);

File (sitedoc.tpl):

&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;drs:import type="javascript" file="{112908-52ecce2d-d837-4912-9bb7-2df81ca5863c}.js" /&gt;
&lt;drs:import type="javascript" file="{112908-52ecce2d-d837-4912-9bb7-2df81ca5863c}.js" /&gt;
&lt;drs:import type="javascript" file="{112908-52ecce2d-d837-4912-9bb7-2df81ca5863c}.js" /&gt;
&lt;/head&gt;

&lt;body&gt;
{DRSTPL_DOC}
&lt;/body&gt;
&lt;/html&gt;

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 sathenzar
Link 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

  • 0
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("/&lt;drs:import[^\/]*\/&gt;/", $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 by C++
  • 0

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/

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.