Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Loop through and process every php code.

Loop through and process every php code.

Scheduled Pinned Locked Moved C#
tutorialphptestingbeta-testingtools
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jafingi
    wrote on last edited by
    #1

    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.

    J 1 Reply Last reply
    0
    • J jafingi

      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.

      J Offline
      J Offline
      Jens Meyer
      wrote on last edited by
      #2

      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

      J 1 Reply Last reply
      0
      • J Jens Meyer

        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

        J Offline
        J Offline
        jafingi
        wrote on last edited by
        #3

        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

        G 1 Reply Last reply
        0
        • J jafingi

          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

          G Offline
          G Offline
          Gideon Engelberth
          wrote on last edited by
          #4

          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 ?

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups