Forum Moderators: coopster

Message Too Old, No Replies

Simple Preg replace

First time use of preg replace

         

Dilly

12:58 pm on Sep 18, 2007 (gmt 0)

10+ Year Member



I would like to know how I would go about changing my links throughout a document using preg_replace.

For example.

I have links called foo.php throughout my document I want to change it to new.php how would I go about doing this using preg_replace and prevent myself from typing it manually throughout the document.

I have looked at php manual but it doesnt say how to do it throughout a document and not specific a named variable.

Thanx in advance ppl.

eelixduppy

2:20 pm on Sep 18, 2007 (gmt 0)



Do you want to change all of the links or just foo.php? This will change the solution. If it is the latter, then a simple str_replace may be your best bet.

Dilly

2:28 pm on Sep 18, 2007 (gmt 0)

10+ Year Member



I would like to change all links that have foo.html e.g. there maybe 30 links on a page or pages that contain foo.html i need these now to be new.php.

str_replace works like this right?

str_replace($search,$replace,$string);

but how do i use that when the $string is the whole page which is what it needs to search through to find foo.html and replace with new.php.

Omg im a junior member how did that happen w00t!

[edited by: Dilly at 2:32 pm (utc) on Sep. 18, 2007]

eelixduppy

2:32 pm on Sep 18, 2007 (gmt 0)



You can use file_get_contents [us2.php.net]() to grab the whole file as a string and then fwrite [php.net] to write it back to the file. However, if you plan on doing this only once, why not just use your favorite text editor and use the "replace all" feature?

Dilly

2:37 pm on Sep 18, 2007 (gmt 0)

10+ Year Member



Thanx for the swift reply eelix.

The reason why I don't use the replace all on a text editor is because i am looking to use this str_replace thinmy for the foo.html in a header.php include which will perform it on all the god knows how many pages of my website. Could you give me and example of how I would use those two functions with str_replace.

Cheers

Dilly

3:49 pm on Sep 18, 2007 (gmt 0)

10+ Year Member



It's doing my head in.

It saying that file_get_contents undefined function so is there any other way without me having to upgrade my php.

I'm sure this is a common problem that people get sorted when they need to change hrefs throughout there website.

Otherwise I'll have to do it the hard way and find and replace all links throughout the files.

Please don't make me do it!

[edited by: Dilly at 3:50 pm (utc) on Sep. 18, 2007]

jatar_k

3:58 pm on Sep 18, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



what version do you have?

you could use

fread
file with implode

Dilly

4:08 pm on Sep 18, 2007 (gmt 0)

10+ Year Member



Thanx for reply,

version 4.2.3 and how do i use those for my problem jatar_k?

[edited by: Dilly at 4:09 pm (utc) on Sep. 18, 2007]

jatar_k

4:13 pm on Sep 18, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I would suggest an upgrade

they are replacements for file_get_contents, the manual has good examples of how they are used

fread probably being the simplest

use fread to get the contents of the file into a var
use str_replace to swap the links
then fwrite the contents of the var back to the file

I would also suggest a backup before you do this

how many pages are we talking? You might be better off to change all those files to use an include for the header. This would save quite a bit of time in the future when you need to change the header.

whoisgregg

4:21 pm on Sep 18, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Before I upgraded to PHP 5, I used PHP Compat [pear.php.net] and included just the function file that had whatever function I needed. (You can also include the entire library but I didn't see the point in doing that since all I was "missing" at the time was the file_get/put_contents functions.)

Dilly

4:34 pm on Sep 18, 2007 (gmt 0)

10+ Year Member



Thanx for the reply whoisgregg and jatar_k.

I quite frankly don't have the time to upgrade and mess about with all that, so I will try and find some program which will find and replace text within mutliple files.

Dilly

4:36 pm on Sep 18, 2007 (gmt 0)

10+ Year Member



I do use an include header at the moment jatar_k.

Bout 100 or so pages i think.

[edited by: Dilly at 4:36 pm (utc) on Sep. 18, 2007]

Dilly

5:16 pm on Sep 18, 2007 (gmt 0)

10+ Year Member



I have found a great program for webmasters and programmers to use.
It's called TextCrawler.
It's a tool for searching and replacing across multiple text files. Supports regular expressions and provides an expression tester and library facility. It also features an interactive file list and highlighted search results.

and it's FREEWARE!

Google it guys its Baaaad!

[edited by: Dilly at 5:16 pm (utc) on Sep. 18, 2007]

whoisgregg

5:17 pm on Sep 18, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you download PHP Compat, you'll find a folder with a bunch of individual include files for each function it adds the functionality for. Just pull the one(s) for file_get_contents and file_put_contents, throw them into your site folder, and include them in your script. It's dead simple.

jatar_k

5:35 pm on Sep 18, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I can do the same in textpad, 100 files is not too bad

so if you use an include you are just changing the name of the file to include?

SeanW

5:44 pm on Sep 18, 2007 (gmt 0)

10+ Year Member




perl -p -i.bak -e 's/foo.php/new.php/g' *.php

?

Sean

jatar_k

5:49 pm on Sep 18, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



hehe, nice SeanW

Dilly

10:03 pm on Sep 18, 2007 (gmt 0)

10+ Year Member



Thanx again whoisgregg i'll give that a shot.

Sean I'll give that a go I've never used perl before.

Thanx guys but really that tool is good.

[edited by: Dilly at 10:04 pm (utc) on Sep. 18, 2007]