replace spaces between ><
Moderator: General Moderators
- shiznatix
- DevNet Master
- Posts: 2745
- Joined: Tue Dec 28, 2004 5:57 pm
- Location: Tallinn, Estonia
- Contact:
replace spaces between ><
ok i have links where i have spaces in the file name but i have to turn those spaces into underscores. basically i need a preg_replace spaces between the > < characters with a _
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Only spaces between > < ? If it was ALL spaces then a str_replace would do it but...
Code: Select all
function space2underscore($dir='./')
{
$handle = opendir($dir);
while ($old_name = readdir($handle))
{
$new_name = preg_replace('/> </', '>_<', $old_name);
if (rename($dir.$old_name, $dir.$new_name)) echo $old_name.' => '.$new_name.'<br />';
else echo $old_name.' is not writable for PHP<br />';
}
closedir($handle);
}- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Ah I get ya now.... untested
Code: Select all
preg_replace('/>([^<]+)</e', "str_replace(' ', '_', $1)", $string);- shiznatix
- DevNet Master
- Posts: 2745
- Joined: Tue Dec 28, 2004 5:57 pm
- Location: Tallinn, Estonia
- Contact:
Fatal error: Failed evaluating code: str_replace(' ', '_', diet patch.htm)
my code
my code
Code: Select all
preg_replace('/"([^<]+)"/e', "str_replace(' ', '_', $1)", $other);- shiznatix
- DevNet Master
- Posts: 2745
- Joined: Tue Dec 28, 2004 5:57 pm
- Location: Tallinn, Estonia
- Contact:
ohhhhhhh ok what?
the whole error is this:
Parse error: parse error in /home/sites/.../generator.php(996) : regexp code on line 2
Fatal error: Failed evaluating code: str_replace(' ', '_', weight loss patch.htm) in /home/sites/.../generator.php on line 996
i googled it but there is no answer that i have seen
the whole error is this:
Parse error: parse error in /home/sites/.../generator.php(996) : regexp code on line 2
Fatal error: Failed evaluating code: str_replace(' ', '_', weight loss patch.htm) in /home/sites/.../generator.php on line 996
i googled it but there is no answer that i have seen
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
He means use preg_replace_callback()... it's a bit like what I tried only a bit cleaner since it sends the matches into a new function 
$x is the matches that the preg_ comes across (the array where [0] is the whole string, [1] is the first parens etc..) 
EDIT | Dammit I'm such a dumbass.... did the wrong bit
Code: Select all
function space2underscore($x)
{
return str_replace(' ', '_', $x[1]);
}
echo preg_replace_callback('/<a [^>]+>([^<]+)<\/a>/', 'space2underscore', $string);EDIT | Dammit I'm such a dumbass.... did the wrong bit
Code: Select all
function space2underscore($x)
{
return str_replace(' ', '_', $x[1]);
}
echo preg_replace_callback('/<a [^>]*?\bhref="([^>]+)".*?>/', 'space2underscore', $string);
Last edited by Chris Corbyn on Fri Aug 12, 2005 12:32 pm, edited 1 time in total.
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Code: Select all
<?php
$tagPattern = '#<\s*\w*(?:\s+[A-z0-9_]+(?:\s*=\s*(["\']?).*?\\1))*[^>]*?>#s';
function underlineAttributes($match)
{
$final = $match[0];
if(preg_match_all('#\s+[A-z0-9_]+(?:\s*=\s*(["\']?)(.*?)\\1)#si',$final,$matches,PREG_OFFSET_CAPTURE))
{
$matches = array_reverse($matches[2]);
foreach($matches as $elem)
{
list($match,$offset) = $elem;
$end = preg_replace('#\s+#','_',$match);
$final = substr_replace($final, $end, $offset, strlen($match));
}
}
return $final;
}
$test = '<asdf qrf="blah blah blah" bif="1234 5678 901234" check=\'1234 1234\' check2=\'1234 " 1234\' ><asdf qrf="blah blah blah" bif="1234 5678 901234" check=\'1234 1234\' check2=\'1234 " 1234\' >';
$result = preg_replace_callback($tagPattern, 'underlineAttributes', $test);
echo 'Input:
'.$test.'
Output:
'.$result;
?>Code: Select all
Input:
<asdf qrf="blah blah blah" bif="1234 5678 901234" check='1234 1234' check2='1234 " 1234' ><asdf qrf="blah blah blah" bif="1234 5678 901234" check='1234 1234' check2='1234 " 1234' >
Output:
<asdf qrf="blah_blah_blah" bif="1234_5678_901234" check='1234_1234' check2='1234_"_1234' ><asdf qrf="blah_blah_blah" bif="1234_5678_901234" check='1234_1234' check2='1234_"_1234' >- shiznatix
- DevNet Master
- Posts: 2745
- Joined: Tue Dec 28, 2004 5:57 pm
- Location: Tallinn, Estonia
- Contact:
holy crap kiddies. here is what i did
and it worked
Code: Select all
if (preg_match_all('/"([^<]+).htm"/e', $other, $regs))
{
for ($ctr=1; $ctr<count($regs); $ctr++)
{
$other = str_replace($regs[$ctr], str_replace(' ', '_', $regs[$ctr]), $other);
}
}- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia