Loop through and process every php code.
-
Hi, I am currently writing a program to automaticly encrypt PHP code. Hovewer, I need to find every occurences of php code (<?|<?php //content ?>) and loop through the contents of this. So... If I for example enter the string:
Testing <? echo "test"; ?>
<? $variable = "another test"; echo $variable; ?> The script should find the two occurences of PHP code, and return them in two messageboxes. The first one should return MessageBox.Show("echo \"test\";"); etc. So, find the PHP tags, loop through them (a for loop, I assume), and then put the contents into a string variable (so I can just say MessageBox.Show(phpText);). Does anyone have a clue on how to do this? Thanks.
-
Hi, I am currently writing a program to automaticly encrypt PHP code. Hovewer, I need to find every occurences of php code (<?|<?php //content ?>) and loop through the contents of this. So... If I for example enter the string:
Testing <? echo "test"; ?>
<? $variable = "another test"; echo $variable; ?> The script should find the two occurences of PHP code, and return them in two messageboxes. The first one should return MessageBox.Show("echo \"test\";"); etc. So, find the PHP tags, loop through them (a for loop, I assume), and then put the contents into a string variable (so I can just say MessageBox.Show(phpText);). Does anyone have a clue on how to do this? Thanks.
Im not very sure about real code for you now, but i suggest you should take a look into Regualar Expressions. Regular expressions let you identify portions of strings, characters or even patterns of strings in given source strings (e.g. tag-like structures). You can even replace strings of an expressed pattern in a given source string. .Net supports Regular Expressions since the earliest versions. I guess it will suffice. Ask Mr. Google about examples of regular expressions... Sory bro, that I cannot give you any real code. However, i hope my answer leads you to a solutions. Best regards Trollpower
-
Im not very sure about real code for you now, but i suggest you should take a look into Regualar Expressions. Regular expressions let you identify portions of strings, characters or even patterns of strings in given source strings (e.g. tag-like structures). You can even replace strings of an expressed pattern in a given source string. .Net supports Regular Expressions since the earliest versions. I guess it will suffice. Ask Mr. Google about examples of regular expressions... Sory bro, that I cannot give you any real code. However, i hope my answer leads you to a solutions. Best regards Trollpower
-
Yeah.. I know regex's (I've been writing PHP's for years now :)). Just don't know how to do it in C#, I would do it like this in PHP: preg_match_all("#<? (.*?) ?>#si",$content,$matched);
modified on Friday, December 19, 2008 8:44 AM
The MSDN website has a good explanation of the regular expression capabilities in .NET. [^] 99% of what you know will transfer from PHP. The most obvious differences are that you do not start and end the pattern with a delimiter character and that you specify expression options (like the si at the end of your expression) differently. There are also some advanced things you can do with .NET regular expressions, but you probably will not need them here. As for looping through the matches, you will probably use a while loop
using System.Text.RegularExpressions;
string phpFileText = /* load php file*/;
Regex matcher = new Regex(/* your pattern here */,
/* your options here if not in the pattern itself*/);
Match code = matcher.Match(phpFileText);while (code.Success)
{
/* do stuff here */
code = code.NextMatch();
}PS: don't forget that ? is a quantifier in regular expressions, so you need to escape them when searching for a literal ?