Jump to content

weird file and string manipulation challenge


kellz

Recommended Posts

hey all it's your favorite php freak^^ that no one remembers.

 

well anyway for some reason i had some weird idea (for fun) to do something with the php code in a file. I am trying to randomly split the code (add new lines) after each "(" and ")" character. so as an example below

 

for ($array as $items) {

could become

for (
$array as $items) {

or

for ($array as $items
) {

or

for (
$array as $items
) {

..and so on. but this is the sad part.. I have got as far as splitting 1 line and not even that works!

$lines = file(__FILE__);
$com='';$m=0;
foreach ($lines as $line) {
    
for ($i = 0; $i < strlen($line); $i++) {
     if ($line[$i] == "(") {
	    $m=strpos($line, "(");
	    $com = substr($line, 0, $m);
		$com = "$com <br /> " . substr($line, $m, strlen($line));
	}

}

}

 

i'm totally lost! how do i go about doing this? lol i always put myself in stupid situations.

Well I just made the following function and it *looked* like it would work while writing it but now i have some strange error:

 

Fatal error: Can't use function return value in write context in C:\Program Files\EasyPHP 2.0b1\www\dummy.php on line 6
function DoSplit($line) {
  $pos; $pr1; $pr2; $add; $m;
  $O = array(4);
  $O(0)=chr(44); 
  $O(1)=chr(61);
  $O(2)=chr(46);
  $O(3)=chr(38);

  for ($i = 0; $i < strlen($line); $i++) {
  $m = substr($line, $i, 1);
      if ($m = $O(0) || $m = $O(1) || $m = $O(2) || $m = $O(3)) {
              $pr1 = substr($line, 1, $i);
              $pr2 = substr($line, $i+1, strlen($line));
              if (rand(1,2)==2) { $line = $pr1 . " " . chr(95) . "<br />" . $pr2; }
      }
  }

return $line;
}

 

edit: nevermind...my stupid mistake^^ i'm so used to VB.net and i used the arrays like i would in vb.net..  ::)

that half works.. but some code is missing.. here is a sample output:

__FILE__); $com=''; foreach ($lines as $line)
{ if(rand(0,1)
)
{ $com .= str_replace("(", "(
\n", $line)
; }else{ $com .= str_replace(")
", ")

\n", $line)
; } } echo $com; ?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.