homepage Welcome to WebmasterWorld Guest from 54.227.11.45
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Search and replace across directories
Divvy




msg:4620016
 11:06 am on Oct 30, 2013 (gmt 0)

Hello guys,

Maybe someone can help me...
I have thousands of files to edit in my server and this will consume me lots of hours editing all files manually one by one.
So, I had an idea but I don't know if is possible to make... to create a tools to edit all my files automatically.

This is the tree view of directory/files of my website:
directory3
directory3/file.php
...
directory4
directory4/file.php
...
links
link/sitename.php
...


This is the content of /directory3/ and /directory4/ files:
<?php
echo '<a rel="nofollow" href="http://www.domain.com/link/sitenameA.php" target="_blank"><span>Site Name</span></a>
<a rel="nofollow" href="http://www.domain.com/link/sitenameB.php" target="_blank"><span>Site Name</span></a>
<a rel="nofollow" href="http://www.domain.com/link/sitenameC.php" target="_blank"><span>Site Name</span></a>';
?>


This is the content of all files inside the links directory:
Example: http://www.domain.com/link/sitenameA.php
<?
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: http://affdomain.com/?id=code" );
?>


What I need to change is the URL inside of the href="" of all /directory3/ and /directory4/ files.

From:
<a rel="nofollow" href="http://www.domain.com/link/sitenameA.php" target="_blank"><span>Site Name</span></a>

To:
<a rel="nofollow" href="http://affdomain.com/?id=code" target="_blank"><span>Site Name</span></a>

....................

So, is possible to create some kind of script that grabs the Location of the link/sitenameA.php file:
http://affdomain.com/?id=code

And replace with the URL inside of href="" of all /directory3/ and /directory4/ files?
http://www.domain.com/link/sitenameA.php

Sorry if the post is a little confuse but is a little difficult to explain.
But if you didnt understood, please let me know and I will try to explain better.

Waiting for your replies, thanks! :-)

[edited by: phranque at 11:09 pm (utc) on Oct 30, 2013]
[edit reason] unlinked url [/edit]

 

robzilla




msg:4620020
 11:22 am on Oct 30, 2013 (gmt 0)

Can't you download the site and do a search & replace action locally? I recommend Actual Search & Replace if you're on Windows.

penders




msg:4620042
 1:00 pm on Oct 30, 2013 (gmt 0)

Need a programmer


The subject/title should relate to the thread content, as otherwise it doesn't offer any meaning - every thread posted in the PHP forum should "need a programmer". Ironically this thread doesn't even sound like a programming related task!

As robzilla states, this does sound like a search & replace task - in which case the tools already exist.

Readie




msg:4620141
 8:25 pm on Oct 30, 2013 (gmt 0)

Also, peeps who write PHP aren't even programmers! We're scripters!

But that's just being pedantic.

:)

phranque




msg:4620181
 11:17 pm on Oct 30, 2013 (gmt 0)

this thread doesn't even sound like a programming related task


actually it is.
you have to extract the url from the header call in each source file in one directory, and then go into two other directories to replace all occurrences of href attribute values in anchor tags from the local redirect script to the ultimate Location: specified in the redirect script.

you could do this by brute force using search and replace but if you have more than a few dozen redirect scripts it's probably worth writing a script to do this.

you could probably do something with a unix command line using some combination of awk, grep, sed, etc.

penders




msg:4620194
 12:14 am on Oct 31, 2013 (gmt 0)

Ah OK, I see what you're saying now.

However, depending on how many different URLs there are then I still think it could be predominantly a search and replace task.

1. It would be reasonably trivial to write a script (in PHP) to first traverse the /links directory, extract the Location header and to build a list of all the URLs that need to be translated from / to.

2. Then to perform the search/replace on directory3/4 - either automatically with the list
generated, using a (shell) script of some kind. Or manually one by one (with a search and replace tool).

The task of building the list of from/to URLs could even write the (shell) script?

So, how many links/sitename[A].php files are there? (How many URLs?)

explorador




msg:4620323
 2:29 pm on Oct 31, 2013 (gmt 0)

Yes it can be done but I get the description kinda confusing. Yes it's about extracting urls from one place and replace the url of some page or pages in specific places. It's a search and replace task but I can't figure out how conditional it is and the variables it has. By knowing really well the case it should be quite easy.

DrDoc




msg:4620382
 6:19 pm on Oct 31, 2013 (gmt 0)

perl -p -i -e might actually be what you are looking for ...
swa66




msg:4620422
 8:58 pm on Oct 31, 2013 (gmt 0)

Your first step is to truly define exactly what you want to happen, with NO room at all for doubts.

Once you're there, choosing the tool/language and trying it out is the easy part in most cases.

If you have well structured input, you could use xml tools to do this, but let's assume your input isn't up to that.

PHP is probably your worst possible choice to do it as few run it from the command line and letting the httpd have write access to your documentroot is a bad idea (security wise it's walking the high rope without any safeguards at all)

So aside of some command line use of PHP (which can be done), I'd suggest one of the other "languages" or manners. So clever use of unix command line tools can actually edit a whole bunch of files in an eye-blink, but do something wrong with it and it can mess up all your files just as fast :-)

Same goes for perl and the like.

Just to give you some pointers (unix, I don't do windows unless I get paid a lot):
- find can walk over a directory tree (or just a directory), decide if they are up to snuff (like being a file, like having a certain pattern as name, ...) and execute other command(s) for each file.
- sed can edit a file just like vi, but perform it fully automated, so you can use regexps to do the search and replace.

Together that would feel like it would do nearly all what you need.

Now that said, what I'd be worried about is the content itself:
I have thousands of files to edit in my server
that seem to have very similar content ?
If so that's a bad idea in itself. You probably should focus on not having that to start with.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved