...

View Full Version : replacing text problem



graziano
01-31-2007, 05:39 PM
Hello

I have an $output varaiable containing this

<email@abramsandsmith.com> to: adugarte@my.com (windows-1250)
<email@aberfeldy.freeserve.co.uk> to: macosta@my.com (31 Jan 2007 16:14)
<email@bluewavecomputing.com> to: adugarte@my.com (31 Jan 2007 0060)
<email@aahanet.org> to: jguillen@my.com (31 Jan 2007 16:15:47 -0540)


I am trying to replace all spaces between () with an underscore _
How to do that I am going mad trying and trying.

I am trying to obtain this
echo $output should return this

<email@abramsandsmith.com> to: adugarte@my.com (windows-1250)
<email@aberfeldy.freeserve.co.uk> to: macosta@my.com (31_Jan_ 2007_16:14)
<email@bluewavecomputing.com> to: adugarte@my.com (31_Jan_2007_0060)
<email@aahanet.org> to: jguillen@my.com (31_Jan 2007_16:15:47_-0540)


Any help ? Thanks

Alex!
01-31-2007, 06:43 PM
hi, I made this bit of code ages ago to display certain parts of a page as php code. There may be a better way of doing it, but this is one...



<?php
$output = '<email@abramsandsmith.com> to: adugarte@my.com (windows-1250)
<email@aberfeldy.freeserve.co.uk> to: macosta@my.com (31 Jan 2007 16:14)
<email@bluewavecomputing.com> to: adugarte@my.com (31 Jan 2007 0060)
<email@aahanet.org> to: jguillen@my.com (31 Jan 2007 16:15:47 -0540)';

$beg = '(';
$end = ')';
$string = $output;


$newstring = str_replace($end, $beg, $string);
$piece = explode($beg, $newstring);
$total = count($piece);

for($i=1; $i <= $total; $i += 2){ //gets every other part
$up = ($i + 1);

//Change spaces to underscores
if($piece[$i] != ""){ //checks that piece isn't empty

$replace_spaces = str_replace(' ', '_', $piece[$i]);
$piece[$i] = '('.$replace_spaces.')';

$piece[$up];
}
}


foreach($piece as $key=>$output){

if($key %2 == 0){
echo "$output";
}else{
echo "$output<br/>";
}

}


?>



Alex

P.S apologies for the poor commenting. If you need extra explanation PM me

mlseim
01-31-2007, 06:43 PM
My solution below is sort of my own "hack"...
I'm sort of new at regex, so someone can improve on my answer ...



<?php

$string="<email@abramsandsmith.com> to: adugarte@my.com (31 Jan 2007 16:14)";

//pattern for the preg_match ... everything between and including ()
$pattern = "/\(([^`]*?)\)/";

//do the match
preg_match ($pattern, $string, $match);

//replace all spaces with underscores
$replacement = str_replace(" ", "_", $match[0]);

//put the new (contents) back into the string.
$output = preg_replace($match[0], $replacement, $string);

//clean-up the double (( and ))
$output = str_replace("((", "(", $output);
$output = str_replace("))", ")", $output);

echo $output;

?>

Alex!
01-31-2007, 06:51 PM
yup that looks easier and neater xD does it work for the whole output though?

Alex

Mwnciau
01-31-2007, 07:49 PM
another quick way:



while (preg_match('/\(.*?/s.*?\)', $output){ // not sure if its \s or /s
preg_replace('/\((.*?)/s(.*?)\)', '($1_$2)', $output);
}

aedrin
01-31-2007, 10:28 PM
Why not this:



function stripSpaces($matches) {
return str_replace(' ', '_', $matches[1]);
}

// get anything between parenthesis
preg_replace_callback('/\(([^\)]*?)/', 'stripSpaces', $string);

mlseim
01-31-2007, 11:43 PM
ahh... I forgot all about that! Good one!

http://us2.php.net/preg_replace_callback



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum