find and replace in a string help and advice required

Associate
Joined
24 Jul 2004
Posts
1,580
Location
Preston, Lancs
hi,

I have a big list of strings seperated by new lines. They all contain similar information - in fact they are all URLs. My task is to go through each line and add in a bit of extra information based on the URL. For example:

http://www.somesitehere.com/mysite/myfolder
http://www.somesitehere.com/hissite/hisfolder
http://www.somesitehere.com/mysite/myfolder2
http://www.somesitehere.com/mysite/myfolder4
http://www.othersitethere.com/hersite/herfolder

All I need to do is parse the string, check for example it says '/mysite/' somewhere in the string, and then add in:

'http://www.somesitehere.com/mysite/myfolder|client'

I could do this using If statements I'm sure, but the problem is that I have a lot of different strings to replace |client and a lot of URLs to sort.

Can anyone come up with a solution where I dont end up with a massive nest of If else's? Possibly using arrays...

If it helps I am using php 4, and havent slept for close to 25 hours now so I may be overlooking something very obvious.

Thanks for your time :)
 
Code:
<?php

$string = "http://www.somesitehere.com/mysite/myfolder
http://www.somesitehere.com/hissite/hisfolder
http://www.somesitehere.com/mysite/myfolder2
http://www.somesitehere.com/mysite/myfolder4
http://www.othersitethere.com/hersite/herfolder";

$strArr = explode("\n",$string);

for ($x=0;$x<=count($strArr);$x++) {
	if ( strstr($strArr[$x],"mysite") ) {
		$strArr[$x] = $strArr[$x] . "|client";
	}
}

$new_string = implode("\n",$strArr);

?>
 
Back
Top Bottom