hockey97 Posted July 20, 2011 Share Posted July 20, 2011 I know how I can open up a file and that also includes opening up a php file. yet, how can I search for specific code. Like can I search for function tim() and it will select the function tim(){code} and it's code... so that I can delete just that code if I need to or want too. What if I just want to make modifications inside the code of that function... how would one do this? Quote Link to comment https://forums.phpfreaks.com/topic/242471-how-to-open-a-php-file-and-select-certain-php-code/ Share on other sites More sharing options...
verdanmahmood Posted July 20, 2011 Share Posted July 20, 2011 just double click on a php file and it will open in a software like dreamweaver (if u have one).... then simply press Ctrl+F ... and find for anything... Quote Link to comment https://forums.phpfreaks.com/topic/242471-how-to-open-a-php-file-and-select-certain-php-code/#findComment-1245331 Share on other sites More sharing options...
hockey97 Posted July 20, 2011 Author Share Posted July 20, 2011 no, I don't mean using a editor. I mean coding in php fopen type stuff. I want to open a php file and be able to select certain code inside it... or add code to it and then save it. doing all this via http://php.net/manual/en/function.fopen.php (fopen function) that is inside php. Quote Link to comment https://forums.phpfreaks.com/topic/242471-how-to-open-a-php-file-and-select-certain-php-code/#findComment-1245349 Share on other sites More sharing options...
TeNDoLLA Posted July 20, 2011 Share Posted July 20, 2011 You would probably have to use regular expressions for that. And here is a separate forum for regexp's. What for you are needing this kind of functionality? To me this sounds a bit bizarre that one would need this kind of things. Something like this i guess: 1. Open the file and read it to variable 2. Use regex to find the part of the file you want to modify 3. Show it in textarea or similar, edit and replace the found match in the variable with the edited content 4. Save the edited content to file If you want more precise help, maybe post some examples / code of what you want to happen. Quote Link to comment https://forums.phpfreaks.com/topic/242471-how-to-open-a-php-file-and-select-certain-php-code/#findComment-1245361 Share on other sites More sharing options...
hockey97 Posted July 20, 2011 Author Share Posted July 20, 2011 You would probably have to use regular expressions for that. And here is a separate forum for regexp's. What for you are needing this kind of functionality? To me this sounds a bit bizarre that one would need this kind of things. Something like this i guess: 1. Open the file and read it to variable 2. Use regex to find the part of the file you want to modify 3. Show it in textarea or similar, edit and replace the found match in the variable with the edited content 4. Save the edited content to file If you want more precise help, maybe post some examples / code of what you want to happen. I am making a GUI for my clients where they will pass variables to a php file called edit. there be 2 php files one called edit and one call webpage. the GUI will send veriables to the edit php file. this file will generate code if the user requires it too. all depends on what the user wants... they can even want to delete existing code. so lets say the user wants to make a div fade in and out. They will click a button... that basically if 2 things are selected... for instance conditions and effects. well the edit.php will grab these variables that would have no code but a word or phrase.. it will describe the operation the client requests to be done. so he wants to make a div fade in and fade out when it's the guys birthday.. it will have a exit button in the div. so my edit.php will create a if statement and then condition would be when it's the guys birthday. inside the if statement would be the javascript code that will fade in a div. by default the div will be added to the clients webpage but will be hidden. well our javascript code will fade it in. that be it... after all that code is generated ... we will take this code and toss it to the clients webpage which is a php file called webpage.php we will toss it to there and save it. what I mean use open file function to open the php file and then add the code and then save. this is what I want to do. If the client later on wants to delete the div ... we will then go back in the php file find the code we added and then delete it. How can you do this without me having to manully do this. I want my php script to do all this. plus what is this? http://php.net/manual/en/function.create-function.php would that function create a function? Quote Link to comment https://forums.phpfreaks.com/topic/242471-how-to-open-a-php-file-and-select-certain-php-code/#findComment-1245375 Share on other sites More sharing options...
TeNDoLLA Posted July 20, 2011 Share Posted July 20, 2011 Create function creates anonymous functions, see the examples from php.net. I don't think this has nothing to do with what you are trying to achieve. Then again, are you saying you would let users to write PHP code that will be run on your server? If that is the case, it sounds terrible idea. Practically the users can do then whatever they want in your system (malicious acts included). Third of all, what have you achieved so far? I doubt anyone is willing to code that for you here. If you want someone to code this system for you you need to ask it in the freelance forum instead or hire someone to do it. Quote Link to comment https://forums.phpfreaks.com/topic/242471-how-to-open-a-php-file-and-select-certain-php-code/#findComment-1245377 Share on other sites More sharing options...
hockey97 Posted July 20, 2011 Author Share Posted July 20, 2011 Create function creates anonymous functions, see the examples from php.net. I don't think this has nothing to do with what you are trying to achieve. Then again, are you saying you would let users to write PHP code that will be run on your server? If that is the case, it sounds terrible idea. Practically the users can do then whatever they want in your system (malicious acts included). Third of all, what have you achieved so far? I doubt anyone is willing to code that for you here. If you want someone to code this system for you you need to ask it in the freelance forum instead or hire someone to do it. no, I am not allowing anyone to write code to my server. that's be very stupid to allow clients to do that. It allows hackers to have fun with your system. I am having a GUI that I create myself. This GUI will have images and buttons that when clicked it will send veriables with phrases like words. These words will get toss into a function in edit.php that word will let my server php script edit to know exactly what the user wants to do. inside the function I will want to write code where it will generate php code or whatever code it needs to do what the user asks. so php will be for server sided things, javascript will be the one that does effects on the webpage and html and css be the webpage layout which can be colorized and other stuff. the client won't write any code, won't pass any code to my scripts at all. no one will touch any level of code. I will send variables that has only valid word phases and these phrases my php edit script will know exactly what kind of code. like it will know if we need a condition for the code being generate which means to create an if statement. I want my php code to be able to generate a if statement or other php code and javascript and html if needed and also be able to edit them and delete them. after it writes all the code needed it will dump the code to website.php which is the clients webpage so they won't be touching any code at all. it be just images and buttons where they get to click it and it will end variables to my edit.php file which would generate the proper code that is requested and then save it in the website.php file. which is the users webpage... which is a php file that echos javascript and html and css to create a wepage. I know how to program in PHP. I don't know expert, advance things in it... what I mean like I can open files and close them.... I know how to edit each line of a file and even delete any line in a file. I know how to add text to a file at the first line or the last line. I know how to search for specific texts in side a text file. I know all about the php language even the object oriented part where it talks about classes etc. what I don't know is how can I open a program file like .php and read and edit it where I can go and tell the computer to grab this code and that code and delete them or modify them. I know I can search for the code but I would need to give the whole code in question. For instance... lets say we want to delete this: function tom(){code here } if I use the string searches functions in a file and search for tom() it will find tom() in the file and lets say I replace it with " " which is a blank then the file will have this function {code here } now if I left this in the file.. you will see those php errors flare up because the function is still delcared but no function name is delcared etc. I want to know if there is a way to search for just tom() and it will delete the whole function so if it's function tom(){code here} then I should be able to delete all of it so just searching for tom() is should tell php that this is a function and we want to delete everything the function has and it's name completely. without deleting the whole line. also if I wanted to edit code that is inside that function I should be able to edit such code. how can one do this? Quote Link to comment https://forums.phpfreaks.com/topic/242471-how-to-open-a-php-file-and-select-certain-php-code/#findComment-1245381 Share on other sites More sharing options...
TeNDoLLA Posted July 20, 2011 Share Posted July 20, 2011 This example deletes tom() function with the code. You could use preg_match_all() and preg_replace() for the editing. $string = '<?php // Some php function tom() { if (true) { echo "tom in da house"; } } // Some more php ?>'; $pattern = '/function tom(.*){.*}/s'; $replace = ''; $string = preg_replace($pattern, $replace, $string); echo $string; Quote Link to comment https://forums.phpfreaks.com/topic/242471-how-to-open-a-php-file-and-select-certain-php-code/#findComment-1245389 Share on other sites More sharing options...
hockey97 Posted July 20, 2011 Author Share Posted July 20, 2011 This example deletes tom() function with the code. You could use preg_match_all() and preg_replace() for the editing. $string = '<?php // Some php function tom() { if (true) { echo "tom in da house"; } } // Some more php ?>'; $pattern = '/function tom(.*){.*}/s'; $replace = ''; $string = preg_replace($pattern, $replace, $string); echo $string; ok thanks, I will read up more on preg_replace. I didn't know you can select inbetween the { }. but then how can I select code? what I am not understanding is the concept to making a gui where it will select the code. lets say we created the function tom() and saved it in website.php but now the user was like uhhh... don't want this function anymore. I don't want to manually do the code above to replace it with blank. but need some way where I can grab the function name and with my code can generate the code example you gave. so I don't want to do things manually but want the stuff automated. Quote Link to comment https://forums.phpfreaks.com/topic/242471-how-to-open-a-php-file-and-select-certain-php-code/#findComment-1245393 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.