Page 1 of 1

replace spaces between ><

Posted: Fri Aug 12, 2005 10:10 am
by shiznatix
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 _

Posted: Fri Aug 12, 2005 10:30 am
by Chris Corbyn
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);
}

Posted: Fri Aug 12, 2005 10:37 am
by shiznatix
whoa im not doing this to files its just a string. i just have the string

<a href="to some site.php">Cool Text</a>

and i need to turn that into

<a href="to_some_site.php">Cool Text</a>

whoops sorry its actually everything between the "s not the >< sorry

Posted: Fri Aug 12, 2005 10:46 am
by Chris Corbyn
Ah I get ya now.... untested

Code: Select all

preg_replace('/>([^<]+)</e', "str_replace(' ', '_', $1)", $string);

Posted: Fri Aug 12, 2005 10:53 am
by shiznatix
Fatal error: Failed evaluating code: str_replace(' ', '_', diet patch.htm)

my code

Code: Select all

preg_replace('/"([^<]+)"/e', "str_replace(' ', '_', $1)", $other);

Posted: Fri Aug 12, 2005 11:02 am
by feyd
might be best for a callback, me thinks. (secure)

Posted: Fri Aug 12, 2005 11:06 am
by shiznatix
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

Posted: Fri Aug 12, 2005 12:26 pm
by Chris Corbyn
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 ;)

Code: Select all

function space2underscore($x)
{
    return str_replace(' ', '_', $x[1]);
}

echo preg_replace_callback('/<a [^>]+>([^<]+)<\/a>/', 'space2underscore', $string);
$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 :P

Code: Select all

function space2underscore($x)
{
    return str_replace(' ', '_', $x[1]);
}

echo preg_replace_callback('/<a [^>]*?\bhref="([^>]+)".*?>/', 'space2underscore', $string);

Posted: Fri Aug 12, 2005 12:28 pm
by feyd

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;

?>
outputs:

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' >

Posted: Fri Aug 12, 2005 1:03 pm
by shiznatix
holy crap kiddies. here is what i did

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);
  }
}
and it worked

Posted: Fri Aug 12, 2005 1:50 pm
by Chris Corbyn
Good kidda ;)