I've just come back to the pub so I'm going to interpret your question as "Hey 1337, how many different ways can you do this?" ๐
From here on in we'll take $string to contain the string '/home/mypath/abc/xyz/'
Method 1
$string_parts = explode('/', $string);
$string='/';
foreach($string_parts as $part)
if($part!='xyz')
$string.=$part.'/';
Method 2
$string = str_replace('xyz/', '', $string);
Method 3 - Silly way
$source=$string;
$string='';
for($i=0, $current='', $c=strlen($source); $i<$c; $i++) {
if($source[$i]!='/') {
$current.=$source[$i];
} else {
if($current!='xyz')
$current.=$current.'/';
}
}
Method 4 - Really Really Really PHP5 only way (completely untested because it's ridiculous๐)
class StringWrapper {
private $string;
private $position;
public function stream_open($path, $mode, $options, &$opened_oath) {
//We can ignore the path, we're only dealing with variables
$this->string='';
$this->position=0;
return true;
}
public function stream_read($count) {
$count = min($count, strlen($this->string)-$this->position);
$data = substr($this->string, $this->position, $count);
$this->position+=strlen($data);
return $data;
}
public function stream_write($data) {
$this->string+=$data;
$this->position+=strlen($data);
return strlen($data);
}
public function stream_close() {
//nothing to do
}
public function stream_tell() {
return $this->position;
}
public function stream_eof() {
return $this->position>=strlen($this->string);
}
public function stream_seek($offset, $whence) {
switch($whence) {
case SEEK_SET:
if($offset >=0 && $offset<strlen($this->string)) {
$this->position = $offset;
return true;
} else {
return false;
}
break;
case SEEK_CUR:
if($offset>=0 && $this->position + $offset<strlen($this->string)) {
$this->position+=$offset;
return true;
} else {
return false;
}
break;
case SEEN_END:
if(strlen($this->string) + $offset>=0) {
$this->position = strlen($this->length) + $offset;
return true;
} else {
return false;
}
break;
default:
return false;
}
}
}
class removePartFilter extends php_user_filter {
function filter($in, $out, &$consumed, $closing) {
while($bucket = stream_bucket_make_writable($in)) {
$this->data.=$bucket->data;
if(substr_count($this->data, '/') >=2) {
$this->data = str_replace('xyz/', '', $this->data);
stream_bucket_append($out,$this->data);
$this->data='';
}
}
if($closing) {
$this->data.=$bucket->data;
if(substr_count($this->data, '/') >= 2) {
$this->data = str_replace('xyz/', '', $this->data);
}
stream_bucket_append($out, $this->data);
}
return PSFS_PASS_ON;
}
}
stream_register_wrapper('string', 'StringWrapper') or die('Faield to register string wrapper');
stream_filter_register('convert.path', 'removePartFilter') or die('Failed to register filter');
$fp = fopen('string://anything', 'r+');
fwrite($fp, $string);
$string='';
rewind($fp);
stream_filter_prepend($fp, "convert.path");
while(!feof($fp)) {
$string.=fread($fp, 1024);
}
fclose($fp);
I would suggest Method 2 ๐
If anyone else has got any funky ways of doing it bring 'em on :evilgrin: