kellz Posted April 22, 2008 Share Posted April 22, 2008 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. Link to comment https://forums.phpfreaks.com/topic/102344-weird-file-and-string-manipulation-challenge/ Share on other sites More sharing options...
kellz Posted April 22, 2008 Author Share Posted April 22, 2008 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.. Link to comment https://forums.phpfreaks.com/topic/102344-weird-file-and-string-manipulation-challenge/#findComment-524059 Share on other sites More sharing options...
mrdamien Posted April 22, 2008 Share Posted April 22, 2008 More simple: $lines = file(__FILE__); $com=''; foreach ($lines as $line) { if(rand(0,1)){ $com .= str_replace("(", "(<br>\n", $line); }else{ $com .= str_replace(")", ")<br>\n", $line); } } Link to comment https://forums.phpfreaks.com/topic/102344-weird-file-and-string-manipulation-challenge/#findComment-524063 Share on other sites More sharing options...
kellz Posted April 22, 2008 Author Share Posted April 22, 2008 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; ?> Link to comment https://forums.phpfreaks.com/topic/102344-weird-file-and-string-manipulation-challenge/#findComment-524065 Share on other sites More sharing options...
mrdamien Posted April 23, 2008 Share Posted April 23, 2008 I don't understand whats missing. Please explain. Link to comment https://forums.phpfreaks.com/topic/102344-weird-file-and-string-manipulation-challenge/#findComment-524607 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.