Jump to content

[SOLVED] Replace Help


Recommended Posts

And it'll work for any set of letters, by the way, if I change it up a bit.  Here:

 

$str = "There are some letters here!";

$letters = range("a-z");

$numbers = range("1-26");

$str = str_split("", $str);

array_walk($str, 'strtolower'):

foreach ($str as $k=>$v) {

    $pos = array_search($v, $letters);

    $newarr[] = $numbers[$pos];

}

$newstr = implode("", $newarr);

echo $newstr;

   

Link to comment
https://forums.phpfreaks.com/topic/104917-solved-replace-help/#findComment-537047
Share on other sites

function remove_nonalpha($str) {

  $let = range('a-z');

  $str = strtolower($str);

  if (!in_array($str, $let)) {

    return NULL;

  }

  return $str;

}

$str = "There are some letters here!";

 

$str = str_replace($bad, "", $str);

$letters = range("a-z");

$numbers = range("1-26");

$str = str_split("", $str);

array_walk($str, 'remove_nonalpha'):

foreach ($str as $k=>$v) {

    $pos = array_search($v, $letters);

    $newarr[] = $numbers[$pos];

}

$newstr = implode("", $newarr);

echo $newstr;

 

 

Fixed it to remove non-letter characters.

Link to comment
https://forums.phpfreaks.com/topic/104917-solved-replace-help/#findComment-537054
Share on other sites

Rewritten a 3rd time to let any numbers you currently have in there to stay AND to not recreate a $let array every time (saves resources).  You can remove the number thing if you want.

function remove_nonalpha($str, $key, $let = "") {
   if (!isset($let)) {
       $let = range("a-z");
   }
   $str = strtolower($str);
   if (!in_array($str, $let) || !is_numeric($str)) {
     return NULL;
   }
   return $str;
}
$str = "There are some letters here!";

$str = str_replace($bad, "", $str);
$letters = range("a-z");
$numbers = range("1-26");
$str = str_split("", $str);
array_walk($str, 'remove_nonalpha', $letters):
foreach ($str as $k=>$v) {
    $pos = array_search($v, $letters);
    $newarr[] = $numbers[$pos];
}
$newstr = implode("", $newarr);
echo $newstr; 

Link to comment
https://forums.phpfreaks.com/topic/104917-solved-replace-help/#findComment-537065
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.