Imam Ferianto's Blog

Mei 14, 2006

welcome visitor

Filed under: Uncategorized — by iferianto @ 1:14 am
Let’s make science and knowlege (scpecialy computer knowledge) make our life simple

In the first post I will tell you how to find some text inside directories and sub directories
using php scripting engine.

case:

Suddenly i need to replace my script to featin new project that containt new keyword.
sadnews that I have more than 50 file to replace in folder and subfolder.
I think How can I automate this process?

solution:
I have idea to find that file containt keywords then replace text using PHP-script, write new content
and loop inside folder and sub folder

algorithm:
1. opendir folder using default php function opendir()
2. loop readdir using php function readdir()
3. read current entire file using php function fopen(), fread() , filesize(), fclose()
4. search entire content using php regexp function eregi()
5. if keywords found then i goto next step, if not then I goback loop to step 2
6. if found then i replace keyword string in old content and make new content using
php function str_replace()
7. write new content to file using , fopen(), fwrite(), fclose()

practical:

step1 , we need to define which is start folder
and what is keywords and what is replace string

in code above I must replace \ (slash) with / (backslash) that make php more easy to read folder

step 2, make function my_replace_content

the reason i make this function is if we detect file is folder then we can call this function again
and recursivelly read entire dir.
$folderparam is parameter that we can capture from last call

step 3 and next, add opendir and loop with readdir function to read entire dir and soo on

next step, fullsource code that we can write:

last step, implementation in cmd shell:
clic start-> run -> type : cmd , then pres [enter button]

c:\>c:\apache\php\php.exe -q c:\replacestring.php

Tinggalkan sebuah Komentar »

Belum ada komentar.

RSS umpan untuk komentar-komentar dalam tulisan ini. URI Lacak Balik

Tinggalkan Balasan

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Ubah )

Twitter picture

You are commenting using your Twitter account. Log Out / Ubah )

Facebook photo

You are commenting using your Facebook account. Log Out / Ubah )

Connecting to %s

Theme: Toni. Blog pada WordPress.com.

Ikuti

Get every new post delivered to your Inbox.